Corrected sort order and published status on blog index

master
androiddrew 6 years ago
parent 57f96e1b31
commit 60a1284cf8

@ -5,6 +5,7 @@ from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel from wagtail.admin.edit_handlers import FieldPanel
from wagtail.search import index from wagtail.search import index
class BlogIndexPage(Page): class BlogIndexPage(Page):
intro = RichTextField(blank=True) intro = RichTextField(blank=True)
@ -12,6 +13,16 @@ class BlogIndexPage(Page):
FieldPanel('intro', classname="full") 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): class BlogPage(Page):
date = models.DateField("Post date") date = models.DateField("Post date")
@ -27,4 +38,4 @@ class BlogPage(Page):
FieldPanel('date'), FieldPanel('date'),
FieldPanel('intro'), FieldPanel('intro'),
FieldPanel('body', classname="full"), FieldPanel('body', classname="full"),
] ]

@ -9,23 +9,25 @@
<section class="mw7 center"> <section class="mw7 center">
<h1 class="ph3 ph0-l">{{ page.title }}</h1> <h1 class="ph3 ph0-l">{{ page.title }}</h1>
{% for post in page.get_children %} {% for post in blogpages %}
<article class="pv4 bt bb b--black-10 ph3 ph0-l"> {% with post=post.specific %}
<a class="db pv4 ph3 ph0-l no-underline black dim" href="{% pageurl post %}" > <article class="pv4 bt bb b--black-10 ph3 ph0-l">
<div class="flex flex-column flex-row-ns"> <a class="db pv4 ph3 ph0-l no-underline black dim" href="{% pageurl post %}" >
<div class="w-100 w-60-ns pr3-ns order-2 order-1-ns"> <div class="flex flex-column flex-row-ns">
<h1 class="f3 mt0 lh-title">{{ post.title }}</h1> <div class="w-100 w-60-ns pr3-ns order-2 order-1-ns">
<p class="f5 f4-l lh-copy">{{ post.specific.intro }}</p> <h1 class="f3 mt0 lh-title">{{ post.title }}</h1>
</div> <p class="f5 f4-l lh-copy">{{ post.intro }}</p>
<div class="pl3-ns order-1 order-2-ns mb4 mb0-ns w-100 w-40-ns"> </div>
<img src="http://mrmrs.github.io/photos/cpu.jpg" class="db" <div class="pl3-ns order-1 order-2-ns mb4 mb0-ns w-100 w-40-ns">
alt="Photo of a dimly lit room with a computer interface terminal."> <img src="http://mrmrs.github.io/photos/cpu.jpg" class="db"
</div> alt="Photo of a dimly lit room with a computer interface terminal.">
</div> </div>
<p class="f6 lh-copy gray mv0">By <span class="ttu">{{ post.owner }}</span></p> </div>
<time class="f6 db gray">{{ post.specific.date|date }}</time> <p class="f6 lh-copy gray mv0">By <span class="ttu">{{ post.owner }}</span></p>
</a> <time class="f6 db gray">{{ post.date|date }}</time>
</article> </a>
</article>
{% endwith %}
{% endfor %} {% endfor %}
</section> </section>

Loading…
Cancel
Save