[英]Django : Invalid block tag on line 121: 'popular_products', expected 'endblock'. Did you forget to register or load this tag?
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'.第 122 行的块标记无效:“popular_products”,应为“endblock”。 Did you forget to register or load this tag?
您是否忘记注册或加载此标签?
base_tags.py: 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: home_page.html 模板:
{% extends 'shared/_base.html' %}
{% load base_tags %}
{% load i18n %}
{% load render_partial %}
{% load static %}
{% load ratings %}
{% load thumbnail %}
{% block content %}
<div class="row">
<div class="col-lg-12">
<div class="tab-content">
<div id="recent" class="tab-pane fade show active">
<div class="row product-slider">
{% popular_products %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}
popular_product_slider.html popular_product_slider.html
{% for product in popular_products %}
{{product.image}}
{{product.detail}}
{{product.title}}
{% endfor %}
views.py视图.py
def home_page(request):
context = {}
return render(request, 'Home_page.html', context)
you need to place your code in home_page.html in block.您需要将您的代码放在 home_page.html 块中。 xxx is the name of block that you placed in _base.html
xxx 是您放置在 _base.html 中的块的名称
{% block xxx %}
<div class="row">
<div class="col-lg-12">
<div class="tab-content">
<div id="recent" class="tab-pane fade show active">
<div class="row product-slider">
{% popular_products %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}
You have wrongly used {% popular_products %}
.您错误地使用了
{% popular_products %}
。 You should use {% include 'popular_product_slider.html' %}
to implement popular_product_slider.html
into home_page.html
.您应该使用
{% include 'popular_product_slider.html' %}
将popular_product_slider.html
实现到home_page.html
中。
If you use curly template brackets with precentage sign ( {% %}
), then first value is taken as a command.如果您使用带百分号 (
{% %}
) 的大括号,则第一个值将作为命令。 And file didn't recognize popular_products
as command and it raises Error
.并且 file 没有将
popular_products
识别为命令,它引发了Error
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.