Stu*_*ter 2 django django-templates wagtail
我想在我的Wagtail博客索引页面的侧边栏中添加一个类别标签列表.下面的代码确实有效,但遗憾的是它遍历帖子并将所有标签列为单个标签,最终我最终会出现重复的标签.我从Wagtail演示中构建了我的博客,因为它不像我习惯的那样使用视图,我不知道在哪里添加.distinct('tags').
模板
{% for b in blogs %}
{% for tag in b.tags.all %}
<li><a href="{% pageurl b.blog_index %}?tag={{ tag }}" class="btn btn-primary btn-xs"> <i class="glyphicon glyphicon-tag"></i> {{ tag }}<span>{{ tag }}</span></a>
{% if not forloop.last %} {% endif %}
</li>
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
通常进入视图函数的任何逻辑都可以进入页面模型的get_context方法:
from django.contrib.contenttypes.models import ContentType
from taggit.models import Tag
class BlogIndex(Page):
# ...
def get_context(self, request):
context = super(BlogIndex, self).get_context(request)
blog_content_type = ContentType.objects.get_for_model(BlogPage)
context['tags'] = Tag.objects.filter(
taggit_taggeditem_items__content_type=blog_content_type
)
return context
Run Code Online (Sandbox Code Playgroud)
(这里的标记获取代码是从一些内部Wagtail代码改编而来的.)
| 归档时间: |
|
| 查看次数: |
892 次 |
| 最近记录: |