Wagtail is a powerful content management system (CMS) built on the Django web framework. It provides a user-friendly interface for managing website content, and it also allows for a high degree of customization. One of the ways you can customize Wagtail is by creating pages with StreamField, which allows you to define a flexible, block-based layout for your page content.
In this article, we will go over how to programmatically create a Wagtail page with StreamField. This is a more advanced topic, so it is assumed that you have some familiarity with Wagtail and Python. However, we will try to explain the concepts in an easy-to-understand way, so even if you are new to Wagtail, you should be able to follow along.
Creating a Wagtail Page Model
The first step in creating a Wagtail page with StreamField is to define a page model. This is done by creating a new class that inherits from wagtail.core.Page and adding the necessary fields and methods. For example:
from wagtail.core.models import Page
from wagtail.core.fields import StreamField
from wagtail.core import blocks
class MyPage(Page):
body = StreamField([
('heading', blocks.CharBlock(classname='full title')),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
])
In this example, we have created a page model called MyPage that has a single StreamField called body. This StreamField is made up of three different block types: CharBlock, RichTextBlock, and ImageChooserBlock. These block types allow the user to add headings, paragraphs, and images to the page, respectively.
Creating a Page Programmatically
Once you have defined your page model, you can create a new instance of the page programmatically using the Page.objects.create() method. For example:
from wagtail.core.models import Page
from myapp.models import MyPage
new\_page = MyPage(
title='My New Page',
body=[
('heading', 'Welcome to My Page'),
('paragraph', 'This is a sample page created programmatically.'),
],
)
new\_page.save()
In this example, we are creating a new instance of the MyPage model with a title of 'My New Page' and a body that consists of a heading and a paragraph. We are then saving the page to the database using the save() method.
Adding Images to the Page
Adding images to a page programmatically is a bit more complex, as you need to first create an Image object and then add it to the page. Here is an example of how to do this:
from wagtail.core.models import Page, Image
from myapp.models import MyPage
from wagtail.images.models import Image as WagtailImage
# Create a new image
new\_image = WagtailImage(
title='My Image',
file=File(open('/path/to/my/image.jpg', 'rb')),
)
new\_image.save()
# Add the image to the page
new\_page = MyPage(
title='My New Page',
body=[
('heading', 'Welcome to My Page'),
('paragraph', 'This is a sample page created programmatically.'),
('image', new\_image),
],
)
new\_page.save()
In this example, we are creating a new Image object with the title 'My Image' and the file /path/to/my/image.jpg. We are then saving the image to the database using the save() method. Finally, we are creating a new instance of the MyPage model with the image added to the body.
In this article, we have gone over how to create a Wagtail page with StreamField programmatically. This allows you to create custom page layouts and add them to the CMS programmatically, which can be useful for creating dynamic or data-driven pages. We hope this article has been helpful in getting you started with creating Wagtail pages programmatically.
References
| Title | URL |
|---|---|
| Wagtail Documentation: StreamField | https://docs.wagtail.io/en/latest/topics/streamfield.html |
| Wagtail Documentation: Page Model | https://docs.wagtail.io/en/latest/topics/pages.html#page-model |
| Wagtail Documentation: Image Model | https://docs.wagtail.io/en/latest/topics/images.html#image-model |