iva*_*123 15 django validation checkbox
我想在我的注册字段中添加一个复选框,用于术语和使用.我怎样才能编写一个干净的方法来验证这一点.
我写了一个干净的方法,我想确保我正确捕获复选框值:
def clean_terms(self):
if self.cleaned_data["terms"] == u'on':
raise forms.ValidationError(
"You have to accept terms&conditions to complete registration"
)
Run Code Online (Sandbox Code Playgroud)
结果,当我填写我的注册表并发布它时,它给了我这个验证错误:
条款和条件:选择有效的选择.on不是可用的选择之一.
那么我怎么能理解选中一个复选框以及如何正确实现一个术语和使用复选框?
我的复选框字段:
terms = forms.ChoiceField(
label="Terms&Conditions",
widget=forms.CheckboxInput()
)
Run Code Online (Sandbox Code Playgroud)
Dan*_*man 22
不要将a ChoiceField
用作单个复选框.用一个BooleanField
.
terms = forms.BooleanField(
error_messages={'required': 'You must accept the terms and conditions'},
label="Terms&Conditions"
)
Run Code Online (Sandbox Code Playgroud)
你甚至不需要一种clean_
方法.
归档时间: |
|
查看次数: |
6558 次 |
最近记录: |