2017-03-08 5 views
0

CMS의 모든 페이지에 양식 (예 : 의견 양식)을 추가 할 수있는 방법이 있습니까? 편집자가 필드를 변경할 수 있도록 Wagtail FormBuilder를 사용하고 싶습니다.Wagtail의 모든 페이지에 formbuilder에서 만든 양식을 어떻게 추가 할 수 있습니까?

첫 번째 아이디어는 사이트 루트 자식으로 사용자 정의 양식 페이지 (AbstractEmailForm에서 상속)를 작성하여 base.html 휴지통 템플리트 태그에로드하는 것입니다. 이 방법으로 페이지 속성에 액세스 할 수 있지만 양식을 렌더링 할 수 없습니다. 여기

내 템플릿 태그입니다 :

@register.assignment_tag(takes_context=True) 
def get_feedback_form(context): 
    return context['request'].site.root_page.get_children().type(FeedbackFormPage).first() 

그리고 이것은 내가 base.html에서 그것을 사용하는 방법이다 : 조각으로 양식을 작성하거나 추가 할 어떻게 든 좋은 것

{% get_feedback_form as feedback_form %} 
... 
{{ feedback_form.specific.title }} <-- this works 
{{ feedback_form.specific.form.as_p }} <-- this doesnt work 

사이트 설정,하지만 그 방법을 찾지 못했습니다.

답변

2

가장 큰 문제는 템플릿에 .form.as_p을 사용하여 양식을 생성하는 방법입니다.

.get_form 함수를 사용하여 양식을 생성해야하지만, 현재 사용자 및 페이지가 이와 같이 인수로 있어야하므로 템플리트 내에서 수행하는 것이 가장 좋습니다.

 form = feedback_form_page.get_form(
     page=feedback_form_page, user=request.user) 

당신은 양식이 여기에 AbstractForm 모델 구축하는 방법을 볼 수 있습니다 https://github.com/wagtail/wagtail/blob/master/wagtail/wagtailforms/models.py#L278

전체 상세한 예를 아래에, 당신은 사이트 설정 모듈에 양식 선택을 작업 할 수있는 방법과 함께. http://docs.wagtail.io/en/v1.13/reference/contrib/settings.html

이 문서의 '편집 핸들러'섹션이 페이지에 링크 할 수있는 좋은 방법을 설명합니다 사이트 설정

의 양식에

링크는 사이트를 참조하는 가정이있는 contrib 모듈을 설정 귀하의 사이트 설정 내부. (models.py에서) http://docs.wagtail.io/en/v1.13/reference/contrib/settings.html?highlight=site%20settings#edit-handlers

예 :이 모델을 설정하면

from wagtail.contrib.settings.models import BaseSetting, register_setting 
# ... 
@register_setting 
class MyCustomSettings(BaseSetting): 
    feedback_form_page = models.ForeignKey(
     'wagtailcore.Page', null=True, on_delete=models.SET_NULL) 

    panels = [ 
     # note the page type declared within the pagechooserpanel 
     PageChooserPanel('feedback_form_page', ['base.FormPage']), 
    ] 

, 당신은 관리자에 작업 할 수있는 변화를 makemigrationmigrate을 수행해야합니다. 그런 다음 설정 메뉴 내에서라는 제목의 하위 메뉴를 볼 수 있습니다 '내 사용자 설정'은이에 포함이 모든 페이지

(그래서이 템플릿에서 재정의 할 수 있습니다) 블록을 추가 연결 양식을 추가

당신의 기본 템플릿 (예 : myapp/templates/base.html).

<!-- Footer --> 
<footer> 
    {% block feedback_form %}{% include "includes/feedback_form.html" %}{% endblock feedback_form %} 
    {% include "includes/footer.html" %} 
</footer> 

는 포함 템플릿 만들기

{% load feedback_form_tags wagtailcore_tags %} 

{% get_feedback_form as feedback_form %} 

<form action="{% pageurl feedback_form.page %}" method="POST" role="form"> 
    <h3>{{ feedback_form.page.title}}</h3> 
    {% csrf_token %} 
    {{ feedback_form.form.as_p }} 
    <input type="submit"> 
</form> 

형태로 페이지를 얻을 수있는 템플릿 태그를 구축

템플릿 태그가 필요 (예. myapp와는/템플릿// feedback_form.html 포함) 페이지의 self.get_form() 함수로 양식을 빌드하십시오. 예 :템플릿 태그 (기본/templatetags/feedback_form)

from django import template 
from myapp.models import MyCustomSettings 

register = template.Library() 
# https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/ 


@register.assignment_tag(takes_context=True) 
def get_feedback_form(context): 
    request = context['request'] 
    my_custom_settings = MyCustomSettings.for_site(request.site) 
    feedback_form_page = my_custom_settings.feedback_form_page.specific 
    form = feedback_form_page.get_form(
     page=feedback_form_page, user=request.user) 
    return {'page': feedback_form_page, 'form': form} 
+0

감사합니다. –