New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

Routable Page Categories And Years

Let's take a look at how we can filter blog posts by their URL. Previously we made a Blog Listing Page with the URL of /blog/. In this tutorial we'll add /blog/category/{category_name}/ and filter pages based on the slug in the URL.

This is a continuation post from the original Routable Page Mixin tutorial. This time we'll add URL parameters and filter pages based on a custom route.

Below is the code you can use. Please note, it's purposely not complete. I didn't handle missing categories in the try/except block — that's for you to use as needed and replace with what your application needs.

``` class BlogListingPage(RoutablePageMixin, Page): # ... fields and panels here @route(r"^year/(\d+)/(\d+)/$", name="blogs_by_year") def blogs_by_year(self, request, year=None, month=None): context = self.get_context(request) # Implement your BlogDetailPage filter. Maybe something like this: # if year is not None and month is not None: # posts = BlogDetailPage.objects.live().public().filter(year=year, month=month) # else: # # No year and no month were set, assume this is july-2019 only posts # posts = BlogDetailPage.objects.live().public().filter(year=2019, month=07) # print(year) # print(month) # context["posts"] = posts # Note: The below template (latest_posts.html) will need to be adjusted return render(request, "blog/latest_posts.html", context) @route(r"^category/(?P<cat_slug>[-\w]*)/$", name="category_view") def category_view(self, request, cat_slug): """Find blog posts based on a category.""" context = self.get_context(request) try: # Look for the blog category by its slug. category = BlogCategory.objects.get(slug=cat_slug) except Exception: # Blog category doesnt exist (ie /blog/category/missing-category/) # Redirect to self.url, return a 404.. that's up to you! category = None if category is None: # This is an additional check. # If the category is None, do something. Maybe default to a particular category. # Or redirect the user to /blog/ ¯\_(ツ)_/¯ pass context["posts"] = BlogDetailPage.objects.live().public().filter(categories__in=[category]) # Note: The below template (latest_posts.html) will need to be adjusted return render(request, "blog/latest_posts.html", context) ```
The Git Commit

Want to see the entire git commit? No problem! https://github.com/CodingForEverybody/learn-wagtail/commit/1e21b345cd4f8cdfbc1a3fdd9bb53234aaacb820

Related tutorials

Routable Pages

Posted on

Routable Pages allow us to create "subpages" under any regular Wagtail Page. Essentially, we can create pages with urls that aren't accessible through the Wagtail CMS admin. We'll learn how to implement routable pages, how to add additional context to the new page, how to render a new page template, how to reverse the routable page url in the template and how to reverse the routable page url in the Wagtail model.

View lesson, Routable Pages

How to Add a Blog Listing Page, Blog Detail Page, and Custom Context

Posted on

In this (long) lesson we're going to cover three main topics. They all go hand-in-hand with Wagtail CMS so it makes sense to learn them all together. The three topics are: Listing Pages, Detail Pages and Adding Custom Context to your pages.

View lesson, How to Add a Blog Listing Page, Blog Detail Page, and Custom Context

Getting Child Page Properties From a Subclassed Page

Posted on

In Wagtail, and just like in Django, you can subclass classes. In this lesson, we're subclassing a Wagtail Page into 2 child pages. But when we query for all the parent classes, we're also given the child classes in the QuerySet, and the data is somewhat inconsistent because child classes can have unique fields that differ from their parents and siblings.

View lesson, Getting Child Page Properties From a Subclassed Page

Adding, Modifying and Removing Sitemap Entries

Posted on

In this video we are going to learn how to install Wagtail Sitemaps (sitemap.xml) and how to add, remove and modify sitemap data in a Wagtail Page.

View lesson, Adding, Modifying and Removing Sitemap Entries

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