我正在使用Django Form View将大型单页表单拆分为多页表单,每个表单都有单独的模板。我按照文档和其他在线示例,大多数一切进展顺利。
然而,问题是我在表单中需要字段并且它们验证失败,因为事实证明,数据没有被传递回Django(正如通过覆盖' is_valid()验证的那样'并打印表单数据,以及在我的模板中打印{{wizard.form.errors}}。值得注意的是,在分割之前,表单工作正常。
以下是相关代码的缩短版本。如果有人有一些指针会很棒。我不是这方面的专家,原始代码不是我写的,所以我会尽我所能回答任何问题。谢谢!
VIEWS.PY
FORMS = [("venue", RegisterBusinessFormVenue),
("manager", RegisterBusinessFormManager),
("extras", RegisterBusinessFormExtras)]
TEMPLATES = {"venue": "registrationVenue.html",
"manager": "registrationManager.html",
"extras": "registrationExtras.html"}
class RegisterBusinessView(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join('/var/www/', 'logos'))
def get_template_names(self):
return TEMPLATES[self.steps.current]
def done(self, form_list, **kwargs):
return HttpResponseRedirect(reverse_lazy('submission_page_view'))
MODELS.PY
class RegisterBusinessFormVenue(forms.Form):
venue_name = forms.CharField(label='Venue Name', max_length=128)
class RegisterBusinessFormManager(forms.Form):
manager_first_name = forms.CharField(label='First Name', max_length=255)
class RegisterBusinessFormExtras(forms.Form):
website = forms.URLField(label="Website", required=False)
URLS.PY
url(r'^register/', RegisterBusinessView.as_view(FORMS), name='register_business_view')
TEMPLATE
{% extends "base.html" %}
{% load staticfiles %}
{% block stylesheet %}
<link rel="stylesheet" href="{% static 'css/registration.css' %}">
{% endblock stylesheet %}
{% block content %}
<div class="page-header">
<h1>Register your business</h1>
</div>
<form class="form-horizontal" role="form" action="" method="POST">{% csrf_token %}
{{ wizard.management_form }}
{{ wizard.form.errors}}
{{ wizard.form.non_field_errors}}
<legend>Event Venue Information</legend>
<div class="form-group required {% if wizard.form.venue_name.errors %}has-error{% endif %}">
<label for="inputVenueName" class="col-sm-3 control-label">{{ wizard.form.venue_name.label }}</label>
<div class="col-sm-9">
<input type="text" class="form-control"
name="{{ wizard.form.venue_name.name }}"
id="inputVenueName"
{% if wizard.form.venue_name.value %}value="{{ wizard.form.venue_name.value }}"{% endif %}
placeholder="Venue name where events are held"
spellcheck="true">
</div>
</div>
<div class="register-btn-block">
<button type="submit">Register</button>
</div>
</form>
{% endblock content %}
答案 0 :(得分:0)
我回答了自己的问题。这对我来说是一个愚蠢的错误:我没有使用向导提供的id和名称用于我的html标记。在我上面的模板中,我有'label for'和'id'属性,它们应该填充值'{{wizard.form.venue_name.id_for_label}}'。对于'name'属性,应使用'{{wizard.form.venue_name.html_name}}'填充。
找到可以使用的预定义值here。进行这些更改后,表单数据已正确提交给服务器。