有什么方法可以通过在 wagtail 中执行 python 脚本来创建和发布页面吗?
Is there any way to create and publish pages by executing python script in wagtail?
我可以通过以下过程使用 wagtail 管理界面创建和发布页面(我通过继承页面 class 创建的页面)。
class HomePage(Page):
template = 'tmp/home.html'
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['child'] = PatientPage.objects.child_of(self).live()
return context
class PatientPage(Page):
template = 'tmp/patient_page.html'
parent_page_types = ['home.HomePage',]
name = models.CharField(max_length=255, blank=True)
birth_year = models.IntegerField(default=0)
content_panels = Page.content_panels + [
FieldPanel('name'),
FieldPanel('birth_year'),
]
现在,我想自动创建和发布 PatientPage class 的许多页面,并通过 运行 python 脚本将这些页面作为子页面附加到主页。
这已经回答得很好here。但是,这里有一个针对您的情况的更具体的答案,其中包含有关如何使此脚本成为 运行.
的说明
为了 运行 这个自定义命令脚本,只要你需要,你可以将它创建为 custom django-admin command。
示例:my_app/management/commands/add_pages.py
from django.core.management.base import BaseCommand
from wagtail.wagtailcore.models import Page
from .models import HomePage, PatientPage # assuming your models are in the same app
class Command(BaseCommand):
help = 'Creates many pages'
def handle(self, *args, **options):
# 1 - get your home page
home_page = Page.objects.type(HomePage).first() # this will get the first HomePage
# home_page = Page.objects.get(pk=123) # where 123 is the id of your home page
# just an example - looping through a list of 'titles'
# you could also pass args into your manage.py command and use them here, see the django doc link above.
for page_title in ['a', 'b', 'c']:
# 2 - create a page instance, this is not yet stored in the DB
page = PatientPage(
title=page_title,
slug='new-page-slug-%s'page_title, # pages must be created with a slug, will not auto-create
name='Joe Jenkins', # can leave blank as not required
birth_year=1955,
)
# 3 - create the new page as a child of the parent (home), this puts a new page in the DB
new_page = home_page.add_child(instance=page)
# 4a - create a revision of the page, assuming you want it published now
new_page.save_revision().publish()
# 4b - create a revision of the page, without publishing
new_page.save_revision()
你可以运行这个命令使用$ python manage.py add_pages
注意:在 Python 2 上,确保在管理目录和 management/commands 目录中包含 __init__.py
文件,否则将不会检测到您的命令。
我可以通过以下过程使用 wagtail 管理界面创建和发布页面(我通过继承页面 class 创建的页面)。
class HomePage(Page):
template = 'tmp/home.html'
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['child'] = PatientPage.objects.child_of(self).live()
return context
class PatientPage(Page):
template = 'tmp/patient_page.html'
parent_page_types = ['home.HomePage',]
name = models.CharField(max_length=255, blank=True)
birth_year = models.IntegerField(default=0)
content_panels = Page.content_panels + [
FieldPanel('name'),
FieldPanel('birth_year'),
]
现在,我想自动创建和发布 PatientPage class 的许多页面,并通过 运行 python 脚本将这些页面作为子页面附加到主页。
这已经回答得很好here。但是,这里有一个针对您的情况的更具体的答案,其中包含有关如何使此脚本成为 运行.
的说明为了 运行 这个自定义命令脚本,只要你需要,你可以将它创建为 custom django-admin command。
示例:my_app/management/commands/add_pages.py
from django.core.management.base import BaseCommand
from wagtail.wagtailcore.models import Page
from .models import HomePage, PatientPage # assuming your models are in the same app
class Command(BaseCommand):
help = 'Creates many pages'
def handle(self, *args, **options):
# 1 - get your home page
home_page = Page.objects.type(HomePage).first() # this will get the first HomePage
# home_page = Page.objects.get(pk=123) # where 123 is the id of your home page
# just an example - looping through a list of 'titles'
# you could also pass args into your manage.py command and use them here, see the django doc link above.
for page_title in ['a', 'b', 'c']:
# 2 - create a page instance, this is not yet stored in the DB
page = PatientPage(
title=page_title,
slug='new-page-slug-%s'page_title, # pages must be created with a slug, will not auto-create
name='Joe Jenkins', # can leave blank as not required
birth_year=1955,
)
# 3 - create the new page as a child of the parent (home), this puts a new page in the DB
new_page = home_page.add_child(instance=page)
# 4a - create a revision of the page, assuming you want it published now
new_page.save_revision().publish()
# 4b - create a revision of the page, without publishing
new_page.save_revision()
你可以运行这个命令使用$ python manage.py add_pages
注意:在 Python 2 上,确保在管理目录和 management/commands 目录中包含 __init__.py
文件,否则将不会检测到您的命令。