我使用引導選項卡窗格,我需要在每個窗格中顯示帶有特定標籤的帖子,而無需重新加載頁面 更多詳細信息我正在使用Django與W CMS CMS我的應用程序基於在模型文件用Django中的特定標籤顯示頁面
編輯:添加標籤字典上下文
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>
爲什麼不在你的'BlogIndex'類的'get_context'方法中構造一個標籤及其對應頁面的字典?這將更有效率和更少的代碼,然後通過'if'語句在模板中分割 – Dekker
是的,我有一個只是我喜歡用簡單的方式寫它,首先知道它是如何工作的,然後我將替換爲語句 – a7me3D