简体   繁体   English

Django:第 121 行的块标记无效:“popular_products”,应为“endblock”。 您是否忘记注册或加载此标签?

[英]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.

相关问题 Django:第 14 行的块标记无效:“endblock”,预期为“endfor”。 您是否忘记注册或加载此标签? - Django: Invalid block tag on line 14: 'endblock', expected 'endfor'. Did you forget to register or load this tag? 第 8 行的块标记无效:“crsf_token”,应为“endblock”。 您是否忘记注册或加载此标签? - Invalid block tag on line 8: 'crsf_token', expected 'endblock'. Did you forget to register or load this tag? 第 24 行的块标记无效:“form.as_p”,应为“endblock”。 您是否忘记注册或加载此标签? - Invalid block tag on line 24: 'form.as_p', expected 'endblock'. Did you forget to register or load this tag? 第 10 行的块标记无效:“endblock”,预期为“empty”或“endfor”。 您是否忘记注册或加载此标签? django(蟒蛇) - Invalid block tag on line 10: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag? django (python) 无效的块标记:'endblock'。 您是否忘记注册或加载此标签? - Invalid block tag : 'endblock'. Did you forget to register or load this tag? 无效的块标记:&#39;endblock&#39;。 您是否忘记注册或加载此标签? - Invalid block tag: 'endblock'. Did you forget to register or load this tag? 第8行的块标记无效:“ endblock”。 您是否忘记注册或加载此标签? - Invalid block tag on line 8: 'endblock'. Did you forget to register or load this tag? Django 错误第 21 行的块标记无效:&#39;endblock&#39;。 您是否忘记注册或加载此标签? 我如何解决它 - Django error Invalid block tag on line 21: 'endblock'. Did you forget to register or load this tag? How do i fix it django.template.exceptions.TemplateSyntaxError:第 13 行无效的块标记:'endblock'。 您是否忘记注册或加载此标签? - django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 13: 'endblock'. Did you forget to register or load this tag? TemplateSyntaxError at /music/ 第 6 行的无效块标记:&#39;path&#39;,预期为 &#39;empty&#39; 或 &#39;endfor&#39;。 您是否忘记注册或加载此标签? - TemplateSyntaxError at /music/ Invalid block tag on line 6: 'path', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM