New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

Using Wagtails save() method to remove template caching

In the previous lesson we discovered how to save sections of a template and reduce the number of queries on any given page. But to delete the cache so that Wagtail CMS could update the page was a very manual task. In this tutorial, we'll learn how to use Wagtails save() method to delete specific template fragment cache from our site.

This is second tutorial about template fragment caching. If you're new to template caching, please check out the first tutorial to get an idea of what template caching is and how to apply it.

In the previous tutorial we added template caching to our website, and I demonstrated how it's good and bad. And deleting template cache was a manual job, which is no way to live your life.

In this tutorial we'll learn how to automatically remove template fragment caching when you save a Wagtail Page.

blog/models.py (The Model)
``` from django.core.cache import cache from django.core.cache.utils import make_template_fragment_key from wagtail.core.models import Page class BlogDetailPage(Page): # Your Page fields and panels here... def save(self, *args, **kwargs): """Create a template fragment key. Then delete the key.""" key = make_template_fragment_key( "blog_post_preview", # Matches the name of our template fragment in the blog_listing_page.html [self.id] # Matches the post.id in the template for loop (shown below) ) cache.delete(key) return super().save(*args, **kwargs) class BlogListingPage(Page): # Fields and panels def get_context(self, request, *args, **kwargs): """Adding blog posts to our context.""" context = super().get_context(request, *args, **kwargs) # This is used in our template loop context["posts"] = BlogDetailPage.objects.live().public().order_by('-first_published_at') return context ```
blog/blog_listing_page.html (The Template)
``` {% extends "base.html" %} {% load cache %} {% block content %} {% for post in posts %} {% cache 604800 blog_post_preview post.id %} {{ post.title }} {{ post.url }} {% endcache %} {% endfor %} {% endblock content %} ```
The Git Commit

If you'd like to see the entire Git Commit, with all 12 lines that were added, here's the link: https://github.com/CodingForEverybody/learn-wagtail/commit/f9423929d815f8c3c960b849a57b606728aace0c

And if you want to view the entire repo with all the course progress so far, all the learnwagtail.com video tutorial code is available at https://github.com/CodingForEverybody/learn-wagtail

Related tutorials

How to Add Template Caching

Posted on

Wagtail is a fast CMS. It's built largely for performance, which is why a lot of beautiful features are not enabled by default. In this lesson we're going to take a look at database queries and template fragment caching to speed up our load times (page performance).

View lesson, How to Add Template Caching

Enabling Wagtails Styleguide

Posted on

If you're interested in changing the styling in the /admin/ area of your Wagtail CMS website, or if you're simply looking for a list of Wagtail Icons, the styleguide is where you should be looking. In this tutorial we'll enable the styleguide and look at how some CSS styles are being applied.

View lesson, Enabling Wagtails Styleguide

How to Register a Django Model with Wagtails ModelAdmin

Posted on

In the land of Django and Wagtail, sometimes you need custom Django Models but Wagtail doesn't let you edit these models by default. And personally, I don't believe in giving a client 2 admin dashboards to operate in. So let's add a custom Django Model to our Wagtail website using a Wagtail ModelAdmin.

View lesson, How to Register a Django Model with Wagtails ModelAdmin

Installing Wagtail using Venv

Posted on

This is a clip from the Wagtail for Beginners course. Learn how to setup a brand new Wagtail project using pure Python. No Docker, no virtualenv, no Pipenv, nothing fancy. We'll use Pythons built in venv command.

View lesson, Installing Wagtail using Venv

The Ultimate Wagtail Developers Course

This course covers everything from basic installation to advanced features like custom blocks and API integration, it's perfect for developers looking to enhance their skills with this powerful CMS.

Ultimate Wagtail Developers Course Logo