Wagtail CMS-按相反的时间顺序显示子级

问题描述 投票:0回答:1

我有一个运行Wagtail CMS 2.6.1的网站。用户创建了一个“更新”页面,并在其下创建了许多新闻/更新文章。问题是在“更新”页面上,子代以字母顺序显示,而不是按时间倒序显示。

可以通过管理界面以某种方式更改此设置吗?如果没有,用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
wagtail
1个回答
0
投票

您可以手动对页面进行重新排序(编辑指南中的Reordering pages,但是要使它们按日期自动排序,您需要在代码中进行此操作。

如果“更新页面”仅包含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 %}

(这是关于模型和变量的命名和模板的假设。请随时更改详细信息。)

© www.soinside.com 2019 - 2024. All rights reserved.