用clean
得像
class UserForm(forms.ModelForm): password=forms.CharField(widget=forms.PasswordInput()) confirm_password=forms.CharField(widget=forms.PasswordInput()) class Meta: model=User fields=('username','email','password') def clean(self): cleaned_data = super(UserForm, self).clean() password = cleaned_data.get("password") confirm_password = cleaned_data.get("confirm_password") if password != confirm_password: raise forms.ValidationError( "password and confirm_password does not match" )
def clean(self): cleaned_data = super(UserAccountForm, self).clean() password = cleaned_data.get("password") confirm_password = cleaned_data.get("confirm_password") if password != confirm_password: self.add_error('confirm_password', "Password does not match") return cleaned_data