2017-12-11 15 views
0

아래 프로세스를 사용하여 wagtail admin 인터페이스를 사용하여 페이지 클래스 (상속받은 페이지 클래스로 작성한 페이지)를 작성하고 게시 할 수 있습니다.wagtail에서 python 스크립트를 실행하여 페이지를 만들고 게시 할 수있는 방법이 있습니까?

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 클래스의 여러 페이지의 게시를 자동화하고 파이썬 스크립트를 실행하여 자식으로 홈페이지에 사람들을 추가하고 싶습니다.

+0

아직 시도한 것을 넣으십시오. – Sanket

+0

지금까지 시도한 코드 샘플을 제공해주십시오. 그렇게하면 더 효과적으로 당신을 도울 수 있습니다. – Ivonet

+0

이 답변이 도움이됩니까? https://stackoverflow.com/q/43040023/8070948 –

답변

1

이것은 이미 잘 대답 here입니다. 그러나이 스크립트를 실행 가능하게 만드는 방법에 대한 지침이있는 상황에 대한 구체적인 대답이 있습니다.

필요할 때마다이 사용자 지정 명령 스크립트를 실행하려면이 스크립트를 custom django-admin command으로 만들 수 있습니다.

예 : 당신은 $ python manage.py add_pages

주를 사용하여이 명령을 실행할 수 있습니다

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() 

을 my_app/관리/명령/add_pages.py는 : 파이썬 2에서 모두 관리 __init__.py 파일을 포함해야하고 위와 같이 명령/관리/명령 디렉토리를 사용하면 명령이 감지되지 않습니다.