New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

Restricting Parent and Child Pages

When you start to create several different page types in Wagtail and you create a child page (a page that's nested under a different page) you'll see every single page is an option. In this video we'll explore how to restrict where pages can live by telling the parent page what types of child page(s) they should expect, and by telling the child pages which parent page(s) they can expect.

When you have a lot of different page types in your Wagtail CMS website, you'll eventually "Add a Child Page" and see that every page you have is available for creation.

In most cases, you'll want to restrict these to some degree.

A good example is a Blog Index Page (or Blog Listing Page). Should you be able to add a Home Page under the Blog Index Page? Probably not, because it won't have the same fields as a regular Blog Detail Page, so creating a proper Index page with images, summaries, and other blog related info isn't an option, and your template will look awkward.

To restrict this, we employ 2 methods:

  1. Add a rule to the parent page about what child pages it can have, and
  2. Add a rule to the child page about parent pages it can have

Like all things Wagtail, this is incredibly simple to implement. Each option is one line. That's right. One line of code (per option above) to immediately start guiding your client and/or content entry team about which pages belong where. Let's dive into this.

``` from wagtail.core.models import Page class HomePage(Page): """Home page model.""" subpage_types = [ 'blog.BlogListingPage', # appname.ModelName 'contact.ContactPage', # appname.ModelName 'flex.FlexPage', # appname.ModelName ] parent_page_type = [ 'wagtailcore.Page' # appname.ModelName ] ```

Ok if you're saying "That's more than one line of code!", you're right, it's been broken down on multiple lines for better readability. But they are just Python lists, so you can put them all on one line if you really wanted to.

In the above code we're telling the Home Page to only live under the main Wagtail Core Page (this is the Root page). And we're also saying that the child pages that can live under the Home Page are: Blog Listing Pages, Contact Pages and Flex Pages.

The Git Commit

Want to see all the code from this video? No problem! Here's the link to see the difference between this video.

Related tutorials

How to only get Parent and Child Class Pages and their Specific Fields

Posted on

When querying for a Wagtail Page that has child (Subclassed) pages, you'll receive all of the pages in your QuerySet. There are times when you simply want the child pages (all types), a specific type of child page, or just the parent page itself with no children. In this lesson we'll explore Wagtails .not_exact_type() and .exact_type() methods.

View lesson, How to only get Parent and Child Class Pages and their Specific Fields

Headless CMS: Serializing Child Pages (and QuerySets)

Posted on

In this video we'll add all the blog pages to the blog listing page API response. This can be very helpful for Listing-style pages in your Headless Wagtail website.

View lesson, Headless CMS: Serializing Child Pages (and QuerySets)

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

How to Subclass Wagtail Pages

Posted on

Subclassing is having a class (in this case it's a Wagtail Page) that can be used for other classes (Wagtail Page's). The parent class has all the common attributes for the child pages, and every child page will inherit everything from it's parent. In this lesson we'll explore that by creating a subclassed Article and Video Blog Page that share a common parent, and then we'll extend the functionality of both subclassed pages by adding new fields.

View lesson, How to Subclass Wagtail Pages

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