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

查看:135
本文介绍了除非选中,否则带有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)即使我的名字是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.

推荐答案

initial = True 应该使用 checked = checked 来呈现小部件,所以我在那里看不到问题。表单无效是因为默认情况下所有字段都是必填字段,除非您另外指定。

initial=True should render the widget with checked="checked" so I don't see the problem there.. but the reason the form is invalid is because all fields are required by default unless you specify otherwise.


,因此,如果您传递一个空值-None或空的
字符串()-然后clean()将引发ValidationError异常:

Field.required¶ By default, each Field class assumes the value is required, so if you pass an empty value -- either None or the empty string ("") -- then clean() will raise a ValidationError exception:

如果您想要复选框或其他任何值要是可选的,您需要将 required = False 传递到表单字段构造函数中。

If you'd like a checkbox or any other value to be optional, you need to pass in required=False into the form field constructor.

up_to_date = forms.BooleanField(initial=True, required=False) 
# no longer required..

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

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