Populate latest blog post on homepage

This commit is contained in:
Jake Howard 2022-07-25 20:02:54 +01:00
parent 71c08f5568
commit e7617f24de
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 17 additions and 4 deletions

View file

@ -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

View file

@ -1,6 +1,6 @@
{% extends "wagtail_base.html" %}
{% load wagtailimages_tags %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block main %}
<main {% if page.image %}style="background-image: url({% image_url page.image 'width-1200' %})"{% endif %}>
@ -9,8 +9,10 @@
<input id="search-input" class="input" type="text" placeholder="Search">
</div>
<div class="box latest">
<strong>Latest Post</strong>: <a href="#">{% lorem 6 w random %}</a> &rarr;
</div>
{% if latest_blog_post %}
<div class="box latest">
<strong>Latest Post</strong>: <a href="{% pageurl latest_blog_post %}">{{ latest_blog_post.title }}</a> &rarr;
</div>
{% endif %}
</main>
{% endblock %}