Django表单不调用clean_< fieldname> [英] Django form not calling clean_<fieldname>
问题描述
我正在尝试验证一个表单(以前用于工作)。由于某种原因,当调用form.is_valid()时,我似乎无法获得诸如clean_username(self)之类的各种清理功能。
我知道还没有足够的支票(他们正在建设中看到;-)),但这里是我的课程:
class LoginForm(forms.Form):
username = forms.CharField(max_length = 30)
password = forms.CharField(max_length =
打印清理密码
密码= self.cleaned_data ['密码']
如果密码== u:
raise forms.ValidationError(Password is blank)
返回密码
def clean_username(self):
username = self .cleaned_data ['username']
如果len(username)< 4:
raise forms.ValidationError(Username is less than four charecters)
return username
class RegistrationForm(LoginForm):
confirm_password = forms.CharField(widget = forms.PasswordInput,max_length = 30)
email = forms.EmailField()
def clean_confirm_password(self):
打印清除确认密码
clean_confirm_password = self.cleaned_data ['confirm_password']
如果clean_confirm_password == u:
raise forms.ValidationError(确认密码为空)
return clean_confirm_password
def clean(self):
print在注册表单上运行clean
print self.cleaned_data.items()
password = self.cleaned_data ['password ']
confirm = self.cleaned_data ['confirm_password']
如果password!= confirm:
提交form.ValidationError('密码不匹配')
return self.cleaned_data
谢谢!
我潜入Django之后发现错误的来源 forms.py
来源。 >
似乎如果一个字段留空,该表单将在调用 field.clean()
之后为该字段引发ValidationError ,但是它不调用 clean_< fieldname>
,但它仍然调用该类的main clean方法。为了处理使用这些空白字段的干净方法,您必须执行以下操作:
def clean(self)
try:
password = self.cleaned_data ['password']
#etc
除了KeyError:
raise ValidationError('密码字段为空白')
返回self.cleaned_data
I am attempting to validate a form (and it used to work before). For some reason, I can't seem to get the various cleaning functions such as clean_username(self) to get called when form.is_valid() is called.
I know there are not nearly enough checks yet (they are under construction you see ;-) ), but here are my classes:
class LoginForm(forms.Form):
username = forms.CharField(max_length=30)
password = forms.CharField(max_length=30,widget=forms.PasswordInput)
def clean_password(self):
print "Cleaning password"
password = self.cleaned_data['password']
if password == u"":
raise forms.ValidationError("Password is blank.")
return password
def clean_username(self):
username = self.cleaned_data['username']
if len(username) < 4:
raise forms.ValidationError("Username is fewer than four charecters.")
return username
class RegistrationForm( LoginForm ):
confirm_password = forms.CharField(widget=forms.PasswordInput, max_length=30)
email = forms.EmailField()
def clean_confirm_password(self):
print "Cleaning confirm password"
clean_confirm_password = self.cleaned_data['confirm_password']
if clean_confirm_password == u"":
raise forms.ValidationError("Confirming password is blank.")
return clean_confirm_password
def clean(self):
print "Running clean on a registration form"
print self.cleaned_data.items()
password = self.cleaned_data['password']
confirm = self.cleaned_data['confirm_password']
if password != confirm:
raise forms.ValidationError('Passwords do not match.')
return self.cleaned_data
Thank you!
I discovered the source of the error after diving into the Django forms.py
source.
It seems that if a field is left blank, the form raises a ValidationError for that field after calling field.clean()
, but it then does not call clean_<fieldname>
, but it still calls the main clean method of the class. In order to deal with a clean method that uses those blank fields you have to do the following:
def clean(self):
try:
password = self.cleaned_data['password']
# etc
except KeyError:
raise ValidationError('The password field was blank.')
return self.cleaned_data
这篇关于Django表单不调用clean_< fieldname>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!