2017-02-27 62 views
0

我一直在嘗試使用formtools嚮導在我的Django應用程序中添加一個多頁面的表單。我需要爲每一步設置自定義表單。共有2個步驟。下面是我的代碼:自定義模板不工作在Django的formtools

views.py

TEMPLATES = {"initial": "my_app/form_initial.html", 
     "final": "my_app/form_last.html"} 


class ModelCreateView(SessionWizardView): 
    model = MyModel 
    template_name = "my_app/model_form.html" 
    file_storage =  
    FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'custom')) 

    def get_template_names(self): 
     return [TEMPLATES[self.steps.current]] 

程序my_app/model_form.html

{% extends "my_app/base.html" %} 

{% block extra_css %} 
{{ wizard.form.media }} 
{% endblock %} 

{% block content %} 
<form action="" method="post"> 
{% csrf_token %} 
    {{ wizard.management_form }} 
    <h1>{{ wizard }}</h1> 
    {% if wizard.form.forms %} 
     <h1>{{ wizard.form.forms }}</h1> 
     {{ wizard.form.management_form }} 
     {% for form in wizard.form.forms %} 
      {{ form }} 
     {% endfor %} 
    {% else %} 
     {{ wizard.form }} 
    {% endif %} 
    <input type="submit" value="submit"/> 
</form> 
{% endblock content %} 

程序my_app/form_initial.html

<-- custom form template for step 1 --> 

程序my_app/form_last。 html

<-- custom form template for step 2 --> 

,如果我理解正確,自定義表單模板將取代基本模板的

{{ form }} 

標籤被稱爲(程序my_app/model_form.html)。

我的問題在於,使用當前代碼,直接調用my_app/form_initial.html。

另外,如果我嘗試只是包括

{{ wizard.management_form }} 

在程序my_app/form_initial.html,並附必要的表單元素,上提交,它只是重新加載當前頁面,而不是帶我去my_app應用/ form_last.html

請讓我知道我在這裏做錯了什麼。

UPDATE:我通過正確擴展基本表單模板解決了自定義模板的問題。但現在,當我提交第一步時,同一步驟頁面將重新加載而不是進入下一步。請讓我知道我該如何調試?

更新2:我試着調試更多,發現這個,我的字段沒有得到驗證,即使post請求包含字段,form.is_valid()返回false。 form.errors說所有的字段都是missing

回答

0

我已經實現了模板的方法有2個問題。

  1. 我曾經假定自定義步驟表單模板將在其視圖中定義基本模板後自行替換。正確的方法是定義基本模板模板並將其擴展到自定義步驟模板中。

如:

base_form_template。HTML

{% block content %} 
<form style="height:inherit;" action="" method="post"> 
{% csrf_token %} 
    {{ wizard.management_form }} 
    {{ wizard.form.errors}} 
    {{ wizard.form.non_field_errors}} 
    {% block custom_form %} 
    {% endblock %} 
</form> 
{% endblock content %} 

step1.html

{% extends "my_app/base_form_template.html" %} 

<-- custom form template code goes here --> 

step2.html

{% extends "my_app/base_form_template.html" %} 

<-- custom form template code goes here --> 
  • 以我定製表單模板,所述輸入字段的name屬性需要設置爲​​。這很難找到。我最初只是設置它wizard.form.<model_attribute>,並想知道爲什麼它不起作用。做了更多挖掘並找到了這個。 html_name<step_name>_<field_name>的格式不同。現在看看它有這樣的名字是有道理的。
  • 0

    我得到了嚮導在我的應用程序中使用額外的元組列表映射表單。試試這個,看看它是否有效。

    WIZARD_FORMS = [("initial", InitailFormName), 
           ("final" , FinalFormName), 
          ] 
    
    TEMPLATES = {"initial": "my_app/form_initial.html", 
          "final": "my_app/form_last.html"} 
    
    
    class ModelCreateView(SessionWizardView): 
        model = MyModel 
        template_name = "my_app/model_form.html" 
        file_storage =  
        FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'custom')) 
    
        def get_template_names(self): 
         return [TEMPLATES[self.steps.current]] 
    
    +0

    你是否在任何地方使用'WIZARD_FORMS'變量? –

    +0

    我正在使用WIZARD_FORMS來命名正在使用的表單(並可以選擇跳過一個表單)。我也在嚮導類中有一個變量:form_list = [WizardForm0a,WizardForm2,WizardForm3,WizardForm4,WizardForm5]。我不記得每一節我需要的是什麼,但最後它終於工作了。 –

    +0

    我在urls文件 'url(r'^ my_app/create/$',MyModelCreateView.as_view([(「initial」,MyModelFormInitial), (「final」,MyModelFormLast)]),name =「 model_create「)' 但它似乎並不工作 –