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.
Add a revision as well.
from wagtail.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).
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.
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)
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)
- LanguageRedirectionPage (will redirect to /en)
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,
)
I left you what worked for me, using a django migration:
from django.db import migrations
from wagtail.models import Page, Locale, Site
def create_wagtail_home_page(apps, schema_editor):
HomePage = apps.get_model("blog.HomePage")
ContentType = apps.get_model("contenttypes.ContentType")
# Get the content type for the Page model
page_content_type = ContentType.objects.get(app_label="wagtailcore", model="page")
root_page = Page.objects.order_by("id").first()
default_locale = Locale.objects.first()
home_page = HomePage(title="Home", body="", locale_id=default_locale.id, content_type=page_content_type)
root_page.add_child(instance=home_page)
# Refresh the home page instance, this Must be a PageInstance for the next step to work
home_page = Page.objects.order_by("-id").first()
# Create a site with the new homepage set as the root
Site.objects.create(hostname="localhost", root_page=home_page, is_default_site=True)
def reverse_home_page(apps, schema_editor):
HomePage = apps.get_model("blog.HomePage")
HomePage.objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
("blog", "0002_homepage"),
]
operations = [
migrations.RunPython(create_wagtail_home_page, reverse_home_page),
]
© 2022 - 2024 — McMap. All rights reserved.