Programmatically add a page to a known parent

I would like to create a software subpage for a famous parent. How can i do this? The page will be created in the signal receiver: the page is created when another page is published.

+7
source share
3 answers

Add 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 = models.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 the wagtail does this in the page view of the wagtail (line 156). https://github.com/wagtail/wagtail/blob/stable/1.13.x/wagtail/wagtailadmin/views/pages.py

2018-18-18: 700 , 200 . , . . , , .

+9

:

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

. " Wagtail" LanguageRedirectionPage.

: Wagtail Docs -

:

    • Language RedirectionPage (will be redirected to / ru)
      • Page (ru)
      • 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 to 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,
)
0
source

Source: https://habr.com/ru/post/1016063/


All Articles