Django 表单集不验证

发布于 2024-08-25 10:32:59 字数 1399 浏览 8 评论 0原文

我正在尝试保存表单集,但即使有必填字段,它似乎也绕过了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淤浪 2024-09-01 10:32:59

呵呵,我也遇到了同样的问题。问题是你使用的是表单集!表单集允许表单中的所有字段为空。但是,如果您有 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.

热血少△年 2024-09-01 10:32:59

对于每个必需的字段,在 attrs 参数中添加一个额外的条目


    resident_status = forms.ChoiceField(widget=forms.Select(
        attrs={'class': 'form-control', 'required': 'required'}), choices=President.RESIDENT_STATUS,
        required=True)



如您所见,我为 django 的表单验证保留了 required=True ,但为模板指定了 'required':'required' 来坚持该字段为必需的。

希望有帮助。

For each of the fields that are required, add an extra entry in the attrs parameter


    resident_status = forms.ChoiceField(widget=forms.Select(
        attrs={'class': 'form-control', 'required': 'required'}), choices=President.RESIDENT_STATUS,
        required=True)



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.

夜清冷一曲。 2024-09-01 10:32:59

添加 2 行。

if request.method == 'POST':
  def initial_form_count(self): return 10 # the number of forms
  AlbumFormSet.initial_form_count = initial_form_count
  formset = AlbumFormSet(request.POST, request.FILES)

祝你好运!

Add 2 lines.

if request.method == 'POST':
  def initial_form_count(self): return 10 # the number of forms
  AlbumFormSet.initial_form_count = initial_form_count
  formset = AlbumFormSet(request.POST, request.FILES)

Good luck!

妥活 2024-09-01 10:32:59

使用:

如果没有(formset.errors):...

而不是:

如果 formset.is_valid(): ...

use:

if not any(formset.errors): ...

instead of:

if formset.is_valid(): ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文