以编程方式将页面添加到已知的父页面 [英] 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.
推荐答案
同时添加修订版.
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()
您可以在 wagtailadmin 页面创建视图(第 156 行)中看到 wagtail 如何做到这一点.https://github.com/wagtail/wagtail/blob/stable/1.13.x/wagtail/wagtailadmin/views/pages.py
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
2018-09-18 更新:我建立了一个 700 页的站点,包括 200 个生成的页面.我从未在任何地方添加过初始修订版,也没有编辑抱怨过.第一次手动编辑后,将进行修订.如果您认为可追溯性需要,请继续添加初始修订版.
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.
这篇关于以编程方式将页面添加到已知的父页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!