Django 表单集不验证
我正在尝试保存表单集,但即使有必填字段,它似乎也绕过了 is_valid() 。
为了测试这一点,我有一个简单的表单:
class AlbumForm(forms.Form):
name = forms.CharField(required=True)
视图:
@login_required
def add_album(request, artist):
artist = Artist.objects.get(slug__iexact=artist)
AlbumFormSet = formset_factory(AlbumForm)
if request.method == 'POST':
formset = AlbumFormSet(request.POST, request.FILES)
if formset.is_valid():
return HttpResponse('worked')
else:
formset = AlbumFormSet()
return render_to_response('submissions/addalbum.html', {
'artist': artist,
'formset': formset,
}, context_instance=RequestContext(request))
和模板:
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ formset.management_form }}
{% for form in formset.forms %}
<ul class="addalbumlist">
{% for field in form %}
<li>
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
</li>
{% endfor %}
</ul>
{% endfor %}
<div class="inpwrap">
<input type="button" value="add another">
<input type="submit" value="add">
</div>
</form>
最终发生的是我点击“添加”而不输入名称,然后 HttpResponse('worked') 被调用,似乎假设它是有效的表单。
我可能在这里遗漏了一些东西,但我看不出出了什么问题。我想要发生的是,就像任何其他表单一样,如果该字段未填写,则需要吐出错误。有什么想法吗?
I am trying to save a formset but it seems to be bypassing is_valid() even though there are required fields.
To test this I have a simple form:
class AlbumForm(forms.Form):
name = forms.CharField(required=True)
The view:
@login_required
def add_album(request, artist):
artist = Artist.objects.get(slug__iexact=artist)
AlbumFormSet = formset_factory(AlbumForm)
if request.method == 'POST':
formset = AlbumFormSet(request.POST, request.FILES)
if formset.is_valid():
return HttpResponse('worked')
else:
formset = AlbumFormSet()
return render_to_response('submissions/addalbum.html', {
'artist': artist,
'formset': formset,
}, context_instance=RequestContext(request))
And the template:
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ formset.management_form }}
{% for form in formset.forms %}
<ul class="addalbumlist">
{% for field in form %}
<li>
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
</li>
{% endfor %}
</ul>
{% endfor %}
<div class="inpwrap">
<input type="button" value="add another">
<input type="submit" value="add">
</div>
</form>
What ends up happening is I hit "add" without entering a name then HttpResponse('worked') get's called seemingly assuming it's a valid form.
I might be missing something here, but I can't see what's wrong. What I want to happen is, just like any other form if the field is required to spit out an error if its not filled in. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
呵呵,我也遇到了同样的问题。问题是你使用的是表单集!表单集允许表单中的所有字段为空。但是,如果您有 2 个字段,并且仅填写一个字段,那么它将识别您所需的内容。这样做是因为表单集是为“批量添加”而设计的,有时您不想填写页面上的所有额外表单。真烦人;您可以在此处查看我的解决方案。
Heh, I was having this exact same problem. The problem is that you're using a formset!! Formsets allow all fields in a form to be blank. If, however, you have 2 fields, and fill out only one, then it will recognize your required stuffs. It does this because formsets are made for "bulk adding" and sometimes you don't want to fill out all the extra forms on a page. Really annoying; you can see my solution here.
对于每个必需的字段,在 attrs 参数中添加一个额外的条目
如您所见,我为 django 的表单验证保留了 required=True ,但为模板指定了 'required':'required' 来坚持该字段为必需的。
希望有帮助。
For each of the fields that are required, add an extra entry in the attrs parameter
As you can see, I maintain the required=True for django's form validation but specify 'required':'required' for the template to insist for the field be required.
Hope that helps.
添加 2 行。
祝你好运!
Add 2 lines.
Good luck!
使用:
而不是:
use:
instead of: