New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

Registering Snippets (Blog Category) using Checkboxes

In this tutorial we'll be learning how to register another Wagtail Snippet, but instead of using an Orderable and a SnippetChooserPanel (like the previous lesson) we're going to use a ParentalManyToManyField and a form widget to create Checkboxes in the Wagtail Admin.

Sometimes you want to be able to see every option available from a snippet. A good example is when you select Blog Categories. If you had to select one at a time through a SnippetChooserPanel then you're making the end user click too much, and you're also making it harder for them to see every Blog Category in one glance.

In this tutorial we'll create checkboxes. The end result looks like this:

categories_checkbox_wagtail_cms.png ^ That's a Snippet using checkboxes

Below is a quick cut of the code from this lesson for those who like to copy and paste. For a full view of all the changes you'll want to look at the full Git Commit.

``` """blog/models.py""" from django import forms from django.db import models from modelcluster.fields import ParentalManyToManyField from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel from wagtail.core.models import Page from wagtail.snippets.models import register_snippet @register_snippet class BlogCategory(models.Model): """Blog category for a snippet.""" name = models.CharField(max_length=255) slug = models.SlugField( verbose_name="slug", allow_unicode=True, max_length=255, help_text='A slug to identify posts by this category', ) panels = [ FieldPanel("name"), FieldPanel("slug"), ] class Meta: verbose_name = "Blog Category" verbose_name_plural = "Blog Categories" ordering = ["name"] def __str__(self): return self.name class BlogListingPage(Page): """Listing page lists all the Blog Detail Pages.""" # ... def get_context(self, request, *args, **kwargs): """Adding custom stuff to our context.""" context = super().get_context(request, *args, **kwargs) context["posts"] = BlogDetailPage.objects.live().public() # Example of getting all categories context["categories"] = BlogCategory.objects.all() return context class BlogDetailPage(Page): """Blog detail page.""" # Fields ... categories = ParentalManyToManyField("blog.BlogCategory", blank=True) content_panels = Page.content_panels + [ # ... MultiFieldPanel( [ FieldPanel("categories", widget=forms.CheckboxSelectMultiple) ], heading="Categories" ), # ... ] ```

Related tutorials

Registering Snippets using Django Models

Posted on

Snippets are Wagtails way of re-using existing data. For example, being able to select a Blog Author (or multiple authors) instead of having to add a name, image and website for every blog author in every blog post you make; instead you can simply fill out a form once, and re-use the final data with a couple of clicks.

View lesson, Registering Snippets using Django Models

Using a SnippetChooserPanel to Select Multiple Blog Authors (Snippets + Orderables)

Posted on

This is part 2, coming from the previous article/video. In this lesson we'll be using a SnippetChooserPanel to select Blog Authors (a snippet we registered last episode). We'll also add in an Orderable to our Blog Detail Page so we can select between 1 and 4 Blog Authors.

View lesson, Using a SnippetChooserPanel to Select Multiple Blog Authors (Snippets + Orderables)

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

Routable Page Categories And Years

Posted on

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.

View lesson, Routable Page Categories And Years

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