2017-02-27 65 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“)' 但它似乎并不工作 –