How to add Wagtail page programmatically via Python script

27 Feb ยท by Tim Kamanin ยท 1 min read


Two Wagtail CMS page classes:

class Section(Page):
    subtitle = models.Charfield(max_length=128)

class News(Page):
    content = models.Textfield()


Using a python script, add a News page item under the Section page titled "Latest news."


1) Get parent page instance:

parent_page = Page.objects.get(title='Latest news').specific

2) Create a News page instance:

news_page = News(
    content='Something pleasant to hear.'

3) Add newly created instance as a child to the parent_page:


4) Save news_page:

Now you should see your page in the Wagtail explorer. Done!

Want to get more ๐Ÿ”ฅ tips like this one?

Subscribe to get notified about new dev tutorials