嗨,我在通用 FormView 中使用了表单集,如下所示:
class RequestRecommendationView(FormView):
template_name = "account/stepfour.html"
form_class = formset_factory(StepFourForm)
def form_valid(self,form):
print form.is_valid()
cleaned_data = form.cleaned_data
# return redirect(reverse("some_url_name"))
formset_factory 的形式如下:
class StepFourForm(forms.Form):
contact_person = forms.CharField(required=True)
email = forms.EmailField(required=True)
company = forms.CharField(required=True)
request_message = forms.CharField(required=True)
我的html结构是这样的:
<div style="padding-top:100px;padding-left:10px;">
<h4> Request a Recommendation </h4>
<form method="post" action="">
{% csrf_token %}
<table id="myForm">
<tbody>
{% for f in form %}
{% for field in f %}
<tr>
<td>{{field.label_tag}}</td>
<td>{{field}}</td>
{% for error in field.errors %}
<td><span>{{ error }}</span></td>
{% endfor %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
<button class="btn btn-primary btn-xlarge" type="submit" name="submit">Request Now</button>
{{ form.management_form }}
</form>
</div>
然后我使用 django-dynamic-formset ( https://code.google.com/p/django-dynamic-formset/ ) 通过这些添加/删除额外的表单:
<script type="text/javascript">
$(function() {
$('#myForm tbody').formset();
})
</script>
问题是:如果我将表单留空(每个字段都是必需的),它会设法到达我的类视图的 form_valid() (尽管它不应该),如果我填写其中一个字段(其他字段未填写) ,它成功显示错误关联消息。为什么会这样?我应该怎么做才能解决它?是否提供 form_class 一个表单集是所有这一切的背后?