I have a custom template tag in my project and every thing looks fine but when i want to use this template tag i got this error:
Invalid block tag on line 122: 'popular_products', expected 'endblock'. Did you forget to register or load this tag?
base_tags.py:
from django import template
from django.db.models import Count, Q
from datetime import datetime, timedelta
from shop.models import Product
register = template.Library()
@register.inclusion_tag('shared/partials/popular_product_slider.html')
def popular_products():
last_week = datetime.today() - timedelta(days=7)
return {
"popular_products": Product.objects.filter(available=True).annotate(
count=Count('hits', filter=Q(producthit__date__gt=last_week))).order_by(
'-count', '-created')[:3]
}
home_page.html template:
{% extends 'shared/_base.html' %}
{% load base_tags %}
{% load i18n %}
{% load render_partial %}
{% load static %}
{% load ratings %}
{% load thumbnail %}
{% block content %}
<div >
<div >
<div >
<div id="recent" >
<div >
{% popular_products %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}
popular_product_slider.html
{% for product in popular_products %}
{{product.image}}
{{product.detail}}
{{product.title}}
{% endfor %}
views.py
def home_page(request):
context = {}
return render(request, 'Home_page.html', context)
CodePudding user response:
At one point you implemented something like {% block content %}
. You have to close it at the end of it:
{% block content %}
# things in that block/segment
{% endblock %} # or better: {% endblock content %}
it may be in your main template or in any you extend/include, like 'base.html'.
CodePudding user response:
you need to place your code in home_page.html in block. xxx is the name of block that you placed in _base.html
{% block xxx %}
<div >
<div >
<div >
<div id="recent" >
<div >
{% popular_products %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}