From 60a1284cf8485ecf16815905e83023df190b4d11 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Fri, 16 Nov 2018 13:34:40 -0500 Subject: [PATCH] Corrected sort order and published status on blog index --- services/cms/blog/models.py | 13 ++++++- .../blog/templates/blog/blog_index_page.html | 36 ++++++++++--------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/services/cms/blog/models.py b/services/cms/blog/models.py index 34f5208..f5d40b3 100644 --- a/services/cms/blog/models.py +++ b/services/cms/blog/models.py @@ -5,6 +5,7 @@ from wagtail.core.fields import RichTextField from wagtail.admin.edit_handlers import FieldPanel from wagtail.search import index + class BlogIndexPage(Page): intro = RichTextField(blank=True) @@ -12,6 +13,16 @@ class BlogIndexPage(Page): FieldPanel('intro', classname="full") ] + def get_context(self, request, *args, **kwargs): + """ + Overrides the default context to include only published pages, ordered by reverse chronological order. + These child pages can be accessed in the template using `blogpages` instead of page.get_children. + """ + context = super().get_context(request) + blogpages = self.get_children().live().order_by("-first_published_at") + context["blogpages"] = blogpages + return context + class BlogPage(Page): date = models.DateField("Post date") @@ -27,4 +38,4 @@ class BlogPage(Page): FieldPanel('date'), FieldPanel('intro'), FieldPanel('body', classname="full"), - ] \ No newline at end of file + ] diff --git a/services/cms/blog/templates/blog/blog_index_page.html b/services/cms/blog/templates/blog/blog_index_page.html index dd6af10..af2b890 100644 --- a/services/cms/blog/templates/blog/blog_index_page.html +++ b/services/cms/blog/templates/blog/blog_index_page.html @@ -9,23 +9,25 @@

{{ page.title }}

- {% for post in page.get_children %} - + {% for post in blogpages %} + {% with post=post.specific %} + + {% endwith %} {% endfor %}