我正在制作一个包含标签。在超级shell中,标签返回适当的数据集nevertheles,我没有看到在调用模板上呈现的包含模板。我只能猜测包含模板位于错误的位置。截至目前,模板位于MYPROJECT / templates,这是TEMPLATE_DIRS中的唯一文件夹。请帮我弄清楚我在这里做错了什么。 TIA!
MYPROJECT / newsroom / templatetags / blog_extras.py - > http://pastebin.com/ssuLuVUq
from mezzanine.blog.models import BlogPost
from django import template
register = template.Library()
@register.inclusion_tag('featured_posts.html')
def featured_posts_list():
"""
Return a set of blog posts whose featured_post=True.
"""
blog_posts = BlogPost.objects.published().select_related("user")
blog_posts = blog_posts.filter(featured_post=True)
# import pdb; pdb.set_trace()
return {'featured_posts_list': blog_posts}
MYPROJECT / templates / featured_posts.html - > http://pastebin.com/svyveqq3
{% load blog_tags keyword_tags i18n future %}
Meant to be the the infamous "Featured Posts" Section!
{{ featured_posts_list.count }}
<ul>
{% for featured_post in featured_posts_list %}
<li> {{ featured_post.title }} </li>
{% endfor %}
</ul>
MYPROJECT / settings.py - &gt; pastebin.com/Ed53qp5z
MYPROJECT / templates / blog / blog_post_list.html - &gt; pastebin.com/tJedXjnT
答案 0 :(得分:2)
正如@Victor Castillo Torres所说,您需要更改正在加载的标记的名称,这将修复模板标记的这一方面。但是,即使它们位于不同的命名空间中,我仍然会更改标记返回的上下文变量的名称,只是为了理智:
@register.inclusion_tag('featured_posts.html')
def featured_posts_list():
blog_posts = BlogPost.objects.published().filter(
featured_post=True).select_related("user")
return {'blog_posts': blog_posts}
然后在你的模板中:
{{ blog_posts.count }}
<ul>
{% for blog_post in blog_posts %}
<li>{{ blog_post.title }} </li>
{% endfor %}
</ul>
最后,在您的主模板中:
{% load blog_extras keyword_tags i18n_future %}
...
{% featured_posts_list %}