除非选中,否则带有 BooleanField 的 Django 表单始终无效 [英] Django form with BooleanField always invalid unless checked

查看:11
本文介绍了除非选中,否则带有 BooleanField 的 Django 表单始终无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下形式的应用程序:

I have an application that uses the following form:

class ConfirmForm(forms.Form):
    account_name = forms.CharField(widget=forms.HiddenInput)
    up_to_date = forms.BooleanField(initial=True)

我使用以下模板摘录中的表单:

I use the form in the following template exerpt:

<form class="confirmform" action="/foo/" enctype="multipart/form-data" method="post">
{{ confirm_form.up_to_date }} Check if this data brings the account up to date.<br>
{{ confirm_form.account_name }} <input type="submit" name="confirm" value="Confirm" />
</form>

我的视图使用以下基本代码结构:

My view uses the following basic code structure:

if request.method == 'POST':
    #check for 'confirm' because I actually have multiple forms in this page
    if 'confirm' in request.POST:
        confirm_form = ConfirmForm(request.POST)
        if confirm_form.is_valid():
            #do stuff
        else:
            c['confirm_form'] = confirm_form
else:
    c['confirm_form'] = ConfirmForm({'account_name':'provided_value'})

有两点不对:

1) 即使我有 initial=True,页面加载时复选框也没有被选中

1) Even though I have initial=True, the checkbox is unchecked when the page loads

2) 除非我选中复选框,否则表单始终无效.它仅针对 up_to_date 给出错误:此字段是必需的."

2) The form is always invalid unless I check the checkbox. It gives errors for up_to_date only: "This field is required."

我已阅读这个类似的问题,但他的解决方案不适用到我的项目.

I have read this similar question but his solution doesn't apply to my project.

所以……发生了什么?

我更新了上面的代码以更忠实于我的实际代码.

I updated the code above to be more faithful to my actual code.

问题 1 是我的错,因为我在实例化表单时通过绑定数据覆盖了初始值.

Problem #1 was my fault because I was overriding the initial value by binding data when the form was instantiated.

问题#2 我仍然考虑一个问题.在 up_to_date 上使用 required=False 将解决问题,但是使用默认小部件似乎不正确,BooleanField 可以是NULL(导致有效性检查失败)或 True 但绝不是 False.

Problem #2 I still consider an issue. Using required=False on up_to_date will fix the problem, however it doesn't seem correct that using the default widget, a BooleanField can be either NULL (causes validity check to fail) or True but never False.

推荐答案

在 Django 表单中,必须使用 required=False 创建布尔字段.

In Django forms, Boolean fields must be created with required=False.

当复选框未被选中时,浏览器不会在请求的 POST 参数中发送该字段.如果不指定该字段是可选的,Django 会在 POST 参数中将其视为缺失字段.

When the checkbox isn't checked, browsers do not send the field in the POST parameters of requests. Without specifying that the field is optional Django will treat it as a missing field when not in the POST parameters.

恕我直言,对于布尔表单字段,Django 默认具有这种行为会很好..

Imho it would be nice for Django to have this behavior by default for boolean form fields..

(这已经在 Yuji 的评论中回答了,但作为答案会很有用)

(this was already answered in comments by Yuji but it would be useful as an answer)

这篇关于除非选中,否则带有 BooleanField 的 Django 表单始终无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆