From e7617f24de131178d2cfce1090bc67a9fd43a39b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 25 Jul 2022 20:02:54 +0100 Subject: [PATCH] Populate latest blog post on homepage --- website/home/models.py | 11 +++++++++++ website/home/templates/home/home_page.html | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/website/home/models.py b/website/home/models.py index c56b698..6abfba7 100644 --- a/website/home/models.py +++ b/website/home/models.py @@ -1,7 +1,9 @@ from django.db import models +from django.http.request import HttpRequest from wagtail.admin.panels import FieldPanel from wagtail.images import get_image_model_string +from website.blog.models import BlogPostPage from website.common.models import BasePage @@ -17,3 +19,12 @@ class HomePage(BasePage): FieldPanel("heading"), FieldPanel("image"), ] + + def get_context(self, request: HttpRequest) -> dict: + context = super().get_context(request) + context["latest_blog_post"] = ( + BlogPostPage.objects.live() # type:ignore[attr-defined] + .defer_streamfields() + .latest("date") + ) + return context diff --git a/website/home/templates/home/home_page.html b/website/home/templates/home/home_page.html index 75e9807..cddaae2 100644 --- a/website/home/templates/home/home_page.html +++ b/website/home/templates/home/home_page.html @@ -1,6 +1,6 @@ {% extends "wagtail_base.html" %} -{% load wagtailimages_tags %} +{% load wagtailcore_tags wagtailimages_tags %} {% block main %}
@@ -9,8 +9,10 @@ -
- Latest Post: {% lorem 6 w random %} → -
+ {% if latest_blog_post %} +
+ Latest Post: {{ latest_blog_post.title }} → +
+ {% endif %}
{% endblock %}