modelform django validation code example

Example 1: django forms error customize

class AccountForm(forms.Form):
    email = forms.EmailField(max_length=255)
    username = forms.CharField(max_length=40)
    password = PasswordField(label="Password")
    password_confirm = PasswordField(label="Password")

    def clean(self):
        cd = self.cleaned_data
        if cd.get('password') != cd.get('password_confirm'):
            self.add_error('password_confirm', "passwords do not match !")
        return cd

Example 2: django forms error customize

class AccountForm(forms.Form):
    ....your stuff

    def clean_password(self):
        if self.data['password'] != self.data['password_confirm']:
            raise forms.ValidationError('Passwords are not the same')
        return self.data['password']