我的问题是,我需要动态标签列表在我的所有网页和所有帖子(一个帖子)的内容也在该网页上。如何使用基于类的视图在所有页面上包括标记侧边栏?谢谢。
编辑:标签列表必须根据使用频率进行排序。我的代码:
class AllTagCloudView(ListView):
model = Tag
template_name = 'tag_cloud.html'
context_object_name = 'tag_cloud'
def get_queryset(self):
qs = Tag.objects.values("name", "slug").annotate(Count("post")).order_by('-post__count')
return qs我试着用
@register.inclusion_tag('tag_cloud.html', takes_context=True)
def sidebar_sorted_tags(context):但我不知道怎么做才能起作用。
此外,我还尝试使用{%包括'tag_cloud.html‘%}:
<div>
<p>Tags</p>
{% for tag in tag_cloud %}
<ul>
<li><a href="/tag/{{ tag.get_absolute_url }}">{{ tag.name }}</a></li>
</ul>
{% empty %}
<a href="">There is no tags yet</a>
{% endfor %}
</div>我觉得这是愚蠢的或者我做错了什么。
发布于 2015-02-04 11:56:33
此任务与基于类的视图无关。您需要使用自定义包含模板标签。
@register.inclusion_tag('tag_cloud.html')
def sidebar_sorted_tags():
return {'tag_cloud': Tag.objects.values("name", "slug")
.annotate(Count("post")).order_by('-post__count')}现在在您的base.html模板中编写:
{% load my_tags %}
{% sidebar_sorted_tags %}https://stackoverflow.com/questions/28320751
复制相似问题