1.如果form想单独验证一个字段可以定义如下函数:def clean_password2
,password2
就是定义的字段名称:
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields =('username','email')
def clean_password2(self):
cd = self.cleaned_data
if cd["password"] !=cd["password2"]:
raise forms.ValidationError("密码不一致")
else:
return cd["password2"]
系统先调用系统的clean_data
函数然后在调用自定义的clean_xxx
在form.is_valid()
函数调用的时候会触发自定义的clean_xxx
网友评论