0
votes

I have a web site running Wagtail CMS 2.6.1. Users have created an "Updates" page and a number of news/updates articles under it. The problem is on the "Updates" page the children are shown in alphabetical order, instead of reverse chronological order.

Can this be changed somehow from the admin interface? If not, what is the fastest way to do it in Python?

This is the the Python model (I believe):

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
1
Please post the relevant models. - Dan Swain
thanks @DanSwain, added the model to the question - Access Denied
Also post the UpdatesPage and the template that is rendering this page. - Dan Swain

1 Answers

2
votes

You can manually reorder pages (Reordering pages in the Editor's guide), but for them to be automatically ordered by date, you need to do this in code.

If your 'Updates page' only contains ArticlePage instances as children, then you can add the children sorted by date to the Updates page template context. See Customising template context. It might look like

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

Then in the template, you can use it as

{% for child in children %}
{{ child.title }}
{{ child.date }}
{% endfor %}

(This makes assumptions about your models' and variables' naming and templates. Feel free to change the details.)