【发布时间】:2015-04-03 22:30:38
【问题描述】:
我的问题是我的所有页面上都需要动态标签列表,并且该页面上的所有帖子(一篇帖子)内容也需要。如何使用基于类的视图在所有页面上包含标签侧边栏?谢谢。
编辑: 标签列表必须按使用频率排序。 我的代码:
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):
但我不明白如何做到这一点。
我也尝试过使用 {% include '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>
我认为这很愚蠢,或者我做错了什么。
【问题讨论】:
标签: python django django-templates django-class-based-views