'Programatically add a page to a known parent

I would like to create programatically a sub page for a known parent. How can I do that? The page creation will takes place in a signal receiver: the page is created on publication of another page.



Solution 1:[1]

Add a revision as well.

from wagtail.wagtailcore.models import Page
from models import MyPage

home = Page.objects.get(id=3) # or better Page query
my_page = MyPage(title="test", body="<h1>the body</h1>")
home.add_child(instance=my_page)

# later when a cms user updates the page manually 
# there will be no first revision to compare against unless
# you add a page revision also programmatically.

my_page.save_revision().publish() 

You can see how wagtail does this in the wagtailadmin pages create view (line 156). https://github.com/wagtail/wagtail/blob/stable/1.13.x/wagtail/wagtailadmin/views/pages.py

Update 2018-09-18: I built a 700 page site including 200 generated pages. I never added an initial Revision anywhere and no editors complained. After the first manual edit there will be a Revision. Go ahead and add an initial Revision if you think it is needed for traceability.

Solution 2:[2]

To create a page programmatically:

page = SomePageType(title="My new page", body="<p>Hello world</p>")  # adjust fields to match your page type
parent_page.add_child(instance=page)

Solution 3:[3]

Below is my complete code to create a multi language page structure programatically. It will replace the "Wagtail Welcome Page" with a LanguageRedirectionPage instance.

More information about multi language pages: Wagtail Docs - Internationalization

The page structure is as follows:

  • Page
    • LanguageRedirectionPage (will redirect to /en)
      • Page (en)
      • Page (de)
      • Page (fr)

where the created Site instance at the end of the code points to the LanguageRedirectionPage instance. This is the entry point of our application.

# Deletes existing pages and sites
Site.objects.all().delete()
Page.objects.filter(pk=2).delete() # Deletes Wagtail welcome page
root_page = Page.objects.filter(pk=1).get()

# Adds a LanguageRedirectionPage as a child of the Root Page
app_name = '[Your Project Name]'
page_slug = app_name.lower().replace(" ", "")
sub_root_page = LanguageRedirectionPage(
    title=app_name,
    draft_title=app_name,
    slug=page_slug,
    live=True,
    owner=account,
)

root_page.add_child(instance=sub_root_page)
sub_root_page.save_revision().publish()

# Adds some language pages
for code,caption in dict(settings.LANGUAGES).items():
    print(code, caption)
    sub_root_page.add_child(instance=Page(
        title=caption,
        slug=code,
        live=True,
        owner=account,
    ))

# Adds a new Site instance (See Settings -> Sites in your Wagtail admin panel)
Site.objects.create(
    hostname='localhost',
    port='80',
    site_name=app_name,
    root_page=sub_root_page,
    is_default_site=True,
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 gasman
Solution 3 Bikash kharel