Wagtail CMS - 按时间倒序显示 children
Wagtail CMS - display children in reverse chronological order
我有一个网站 运行 Wagtail CMS 2.6.1。用户创建了一个 "Updates" 页面和其下的一些 news/updates 文章。问题出在 "Updates" 页面上 children 按字母顺序显示,而不是按时间倒序显示。
这可以从管理界面以某种方式改变吗?如果没有,在 Python 中最快的方法是什么?
这是 Python 模型(我相信):
class ArticleIndexPage(Page):
intro = models.CharField(max_length=250, blank=True, null=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname='full')
]
def get_context(self, request, *args, **kwargs):
context = super(ArticleIndexPage, self)\
.get_context(request, *args, **kwargs)
children = ArticlePage.objects.live()\
.child_of(self).not_type(ArticleIndexPage).order_by('-date')
siblings = ArticleIndexPage.objects.live()\
.sibling_of(self).order_by('title')
child_groups = ArticleIndexPage.objects.live()\
.child_of(self).type(ArticleIndexPage).order_by('title')
child_groups_for_layout = convert_list_to_matrix(child_groups)
context['children'] = children
context['siblings'] = siblings
context['child_groups'] = child_groups_for_layout
return context
您可以手动重新排序页面(编辑指南中的Reordering pages),但要按日期自动排序,您需要在代码中执行此操作。
如果您的 'Updates page' 仅包含 ArticlePage
个子实例,则您可以将按日期排序的子实例添加到更新页面模板上下文中。参见 Customising template context。它可能看起来像
class BlogIndexPage(Page):
...
def get_context(self, request):
context = super().get_context(request)
context['children'] = ArticlePage.objects.child_of(self).live().order_by('-date')
return context
然后在模板中,可以作为
{% for child in children %}
{{ child.title }}
{{ child.date }}
{% endfor %}
(这对您的模型和变量的命名和模板做出了假设。请随意更改细节。)
我有一个网站 运行 Wagtail CMS 2.6.1。用户创建了一个 "Updates" 页面和其下的一些 news/updates 文章。问题出在 "Updates" 页面上 children 按字母顺序显示,而不是按时间倒序显示。
这可以从管理界面以某种方式改变吗?如果没有,在 Python 中最快的方法是什么?
这是 Python 模型(我相信):
class ArticleIndexPage(Page):
intro = models.CharField(max_length=250, blank=True, null=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname='full')
]
def get_context(self, request, *args, **kwargs):
context = super(ArticleIndexPage, self)\
.get_context(request, *args, **kwargs)
children = ArticlePage.objects.live()\
.child_of(self).not_type(ArticleIndexPage).order_by('-date')
siblings = ArticleIndexPage.objects.live()\
.sibling_of(self).order_by('title')
child_groups = ArticleIndexPage.objects.live()\
.child_of(self).type(ArticleIndexPage).order_by('title')
child_groups_for_layout = convert_list_to_matrix(child_groups)
context['children'] = children
context['siblings'] = siblings
context['child_groups'] = child_groups_for_layout
return context
您可以手动重新排序页面(编辑指南中的Reordering pages),但要按日期自动排序,您需要在代码中执行此操作。
如果您的 'Updates page' 仅包含 ArticlePage
个子实例,则您可以将按日期排序的子实例添加到更新页面模板上下文中。参见 Customising template context。它可能看起来像
class BlogIndexPage(Page):
...
def get_context(self, request):
context = super().get_context(request)
context['children'] = ArticlePage.objects.child_of(self).live().order_by('-date')
return context
然后在模板中,可以作为
{% for child in children %}
{{ child.title }}
{{ child.date }}
{% endfor %}
(这对您的模型和变量的命名和模板做出了假设。请随意更改细节。)