您好,我在通用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一个formset是所有这些背后吗?
min_num
并在formset_factory中设置validate_min=True
: