在 Django 中显示带有特定标签的页面
Display pages with specific tag in Django
我正在使用 bootstrap 选项卡窗格,我需要在每个窗格中显示带有特定标签的帖子,当然无需重新加载页面
有关更多详细信息,我正在使用 Django 和 Wagtail CMS 我的应用程序基于模型文件
EDIT : 添加标签字典到上下文
models.py:
class BlogIndex(Page):
intro = RichTextField(blank=True)
def get_context(self, request):
base_tags = ['foo','boo','voo']
# Update context to include only published posts, ordered by reverse-chron
context = super(BlogIndex, self).get_context(request)
blogpages = self.get_children().live().order_by('-first_published_at')
context['blogpages'] = blogpages
context['base_tags'] = base_tags
return context
class BlogPageTag(TaggedItemBase):
content_object = ParentalKey('BlogPage', related_name='tagged_items')
class BlogPage(Page):
#info
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#contentpanel ....
注意:我用的是taggit,但是好像没处理好
blog_index.html
<div>
<ul class="nav nav-tabs" role="tablist">
{% for tag in base_tags %}
<li role="presentation" ><a href="#{{tag}}" aria-controls="{{tag}}"
role="tab" data-toggle="tab">{{tag}}</a></li>
{% ednfor %}
</ul>
<!-- Tab panes -->
<div class="tab-content">
{% for tag in base_tags %}
<div role="tabpanel" class="tab-pane" id="{{tag}}">
#this is what i'am thinking of
#for posts in blogpages :
# if post tag == "{{tag}}":
# show post
</div>
</div>
</div>
如果您有一些特定的标签,您可以通过在下面定义它们来在模板中调用它们。
另一方面,如果标签会不断增加,为标签创建一个新的模型可能会更好。
class PostListView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
all_list = Post.objects.all()
news_list = Post.objects.filter(tag='news')
context = {
'all_list': all_list,
'news_list': news_list,
}
return context
编辑:
你可以这样显示;
{% for object in news_list %}
{{ object.title }}
{{ object.timestamp }}
{% endfor %}
我正在使用 bootstrap 选项卡窗格,我需要在每个窗格中显示带有特定标签的帖子,当然无需重新加载页面 有关更多详细信息,我正在使用 Django 和 Wagtail CMS 我的应用程序基于模型文件
EDIT : 添加标签字典到上下文
models.py:
class BlogIndex(Page):
intro = RichTextField(blank=True)
def get_context(self, request):
base_tags = ['foo','boo','voo']
# Update context to include only published posts, ordered by reverse-chron
context = super(BlogIndex, self).get_context(request)
blogpages = self.get_children().live().order_by('-first_published_at')
context['blogpages'] = blogpages
context['base_tags'] = base_tags
return context
class BlogPageTag(TaggedItemBase):
content_object = ParentalKey('BlogPage', related_name='tagged_items')
class BlogPage(Page):
#info
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#contentpanel ....
注意:我用的是taggit,但是好像没处理好
blog_index.html
<div>
<ul class="nav nav-tabs" role="tablist">
{% for tag in base_tags %}
<li role="presentation" ><a href="#{{tag}}" aria-controls="{{tag}}"
role="tab" data-toggle="tab">{{tag}}</a></li>
{% ednfor %}
</ul>
<!-- Tab panes -->
<div class="tab-content">
{% for tag in base_tags %}
<div role="tabpanel" class="tab-pane" id="{{tag}}">
#this is what i'am thinking of
#for posts in blogpages :
# if post tag == "{{tag}}":
# show post
</div>
</div>
</div>
如果您有一些特定的标签,您可以通过在下面定义它们来在模板中调用它们。
另一方面,如果标签会不断增加,为标签创建一个新的模型可能会更好。
class PostListView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super(PostListView, self).get_context_data(**kwargs)
all_list = Post.objects.all()
news_list = Post.objects.filter(tag='news')
context = {
'all_list': all_list,
'news_list': news_list,
}
return context
编辑:
你可以这样显示;
{% for object in news_list %}
{{ object.title }}
{{ object.timestamp }}
{% endfor %}