The Ultimate Wagtail Developers Course

Understanding the Page Model in Wagtail CMS

Get ready to dive into the heart of Wagtail—Page Models! In this lesson, we'll unravel the basic HomePage model that comes pre-packaged with your quick install. We'll explore how this Page Model acts as a blueprint that maps to your database and connects seamlessly to your templates. This is where the magic starts, folks!

Understanding the Page Model in Wagtail CMS

Wagtail CMS, built on top of Django, offers a powerful and flexible way to manage content. One of its core components is the Page Model. In this blog post, we'll dive into what a Page Model is, inspect the HomePage model, and look at how it all connects to the database and your templates. ## What is a Page Model? A Page Model in Wagtail CMS is essentially a Python class that inherits from Wagtail's `Page` class. This model defines the fields and behaviors of the page types in your CMS. It serves as a blueprint for creating and managing similar types of pages in your Wagtail project. ### Why is it Important? The Page Model is crucial because it: - Determines the type of content that can be stored. - Provides methods to manipulate and display the content. - Allows for hierarchical content organization. ## Inspecting the HomePage Model Let's take a look at a simple example of a HomePage model in Wagtail: ```python from wagtail.models import Page from wagtail.fields import RichTextField from wagtail.admin.panels import FieldPanel class HomePage(Page): body = RichTextField(blank=True) content_panels = Page.content_panels + [ FieldPanel('body', classname="full"), ] ``` ### Key Components - **RichTextField**: This defines a rich-text field where you can add styled text, links, and more. - **content_panels**: This is an array that defines the layout of the editing interface in the Wagtail admin. It tells Wagtail which fields should be editable and how they should be displayed. ## Database Mapping The HomePage model maps to a database table where each attribute becomes a column in the table. Wagtail also takes care of creating a primary key for each record and a foreign key for the parent Page model, making the retrieval of hierarchical page data efficient. ## Template Rendering The model connects to a template that lives by default in a folder named `templates` at the root of your Wagtail app. For the HomePage model, you'd typically have a template named `home_page.html`. Here's a simple example: ```html {% extends "base.html" %} {% load wagtailcore_tags %} {% block content %} <h1>{{ page.title }}</h1> <div> {{ page.body|richtext }} </div> {% endblock content %} ``` In this example, `{{ page.body|richtext }}` takes the `body` field from your HomePage model and renders it as HTML. ## Finding All Field Types in Django Docs You can find a comprehensive list of all available field types and their options in the [official Django documentation](https://docs.djangoproject.com/en/4.2/ref/models/fields/).

Community

Comments and questions are hidden until you enroll in this course

Lesson Information

Lesson
Understanding the Page Model in Wagtail CMS
Description
Get ready to dive into the heart of Wagtail—Page Models! In this lesson, we'll unravel the basic HomePage model that comes pre-packaged with your quick install. We'll explore how this Page Model acts as a blueprint that maps to your database and connects seamlessly to your templates. This is where the magic starts, folks!
Duration
16m 47s