django模板在base.html中导入include中再添加自定义标签 错误
原创
©著作权归作者所有:来自51CTO博客作者ch3nnn的原创作品,请联系作者获取转载授权,否则将追究法律责任
base.html
......
<!-- 侧边栏目 -->
{% include 'blog/secondary.html' %}
......
secondary.html
{# 热点文章 #}
{% block hotnews %}
<aside id="views-4" class="widget widget_views"><h3 class="widget-title">Hot Views</h3>
<ul>
{% hot_news_tag as hot_news_list %}
{% for news in hot_news_list %}
<li>
<a href="{{ news.get_absolute_url }}" title="{{ news.title }}">
{{ news.title }}
</a> - 阅读({{ news.views }})
</li>
{% endfor %}
</ul>
</aside>
{% endblock %}
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 32: 'hot_news_tag', expected 'endblock'. Did you forget to register or load this tag?
这里报出错误说 "是否忘记注册或加载此标记?"
解决方式:
在secondary.html 文件首行添加
{% load blog_tags %}
{# 这里需要注册 #}
{% load blog_tags %}
{# 热点文章 #}
{% block hotnews %}
<aside id="views-4" class="widget widget_views"><h3 class="widget-title">Hot Views</h3>
<ul>
{% hot_news_tag as hot_news_list %}
{% for news in hot_news_list %}
<li>
<a href="{{ news.get_absolute_url }}" title="{{ news.title }}">
{{ news.title }}
</a> - 阅读({{ news.views }})
</li>
{% endfor %}
</ul>
</aside>
{% endblock %}