def clean(self) does not work:
It allows user registration, but when I enter different passwords it does not display the error message. {{form.errors}}
from django import forms
from .models import Account
class RegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Ingrese Password',
'class':'form-control',
}))
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Confirmar Password',
'class':'form-control',
}))
class Meta:
model = Account
fields = ['first_name', 'last_name', 'phone_number', 'email', 'password']
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget.attrs['placeholder'] = 'Ingrese nombre'
self.fields['last_name'].widget.attrs['placeholder'] = 'Ingrese Apellidos'
self.fields['phone_number'].widget.attrs['placeholder'] = 'Ingrese Telefono'
self.fields['email'].widget.attrs['placeholder'] = 'Ingrese Email'
for field in self.fields:
self.fields[field].widget.attrs['class']='form-control'
def clean(self):
cleaned_data = super(RegistrationForm, self).clean()
password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password')
if password != confirm_password:
raise forms.ValidationError('La contraseña o coincide')
<div class="form-row">
<div class="form-group col-md-6">
<label>Crear password</label>
{{ form.password}}
</div> <!-- form-group end.// -->
<div class="form-group col-md-6">
<label>Repetir password</label>
{{form.confirm_password}}
</div> <!-- form-group end.// -->
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block"> Registrar</button>
{{form.errors}}
</div> <!-- form-group// -->