我一直在尝试使用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
你是否在任何地方使用'WIZARD_FORMS'变量? –
我正在使用WIZARD_FORMS来命名正在使用的表单(并可以选择跳过一个表单)。我也在向导类中有一个变量:form_list = [WizardForm0a,WizardForm2,WizardForm3,WizardForm4,WizardForm5]。我不记得每一节我需要的是什么,但最后它终于工作了。 –
我在urls文件 'url(r'^ my_app/create/$',MyModelCreateView.as_view([(“initial”,MyModelFormInitial), (“final”,MyModelFormLast)]),name =“ model_create“)' 但它似乎并不工作 –