有什么方法可以通过在wagtail中执行python脚本来创建和发布页面? [英] Is there any way to create and publish pages by executing python script in wagtail?

查看:57
本文介绍了有什么方法可以通过在wagtail中执行python脚本来创建和发布页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用wagtail管理界面通过以下过程来创建和发布页面(我是通过继承Page类创建的).

I can create and publish pages(which I have created by inheriting Page class) by using wagtail admin interface by using the process below.

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类的许多页面,并通过运行python脚本将其作为子代追加到Homepage.

Now, I want to automate creating and publishing of many pages of PatientPage class and append those to Homepage as child by running a python script.

推荐答案

This is already answered well here. However, here is a more specific answer to your situation with instructions on how to make this a script that can be run.

要在需要时运行此自定义命令脚本,可以将其创建为

To run this custom command script whenever you need, you can create this as a custom django-admin command.

示例:my_app/management/commands/add_pages.py

Example: 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上,请确保如上所述在管理目录和管理/命令目录中都包含 __ init __.py 文件,否则将不会检测到您的命令.

Note: On Python 2, be sure to include __init__.py files in both the management and management/commands directories as done above or your command will not be detected.

这篇关于有什么方法可以通过在wagtail中执行python脚本来创建和发布页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆