I am having a problem with my forms. I made a function that checks if the passwords are the same, if this is not the case, it returns a raise with a message. The problem is that this message does not come out. I have 2 other very similar functions with username and email and they work.
Changes: Attach all the code of the forms and also the html. I tried changing the clean function, removing the data, and obtaining the data through self.cleaned_data, but it's still the same. In the other application that I had done in practice mode there was no problem.
I leave the code:
forms.py
class SignupForm(forms.Form):
username = forms.CharField(
min_length=4,
max_length=50,
label='Nombre de usuario'
)
password = forms.CharField(
min_length=4,
max_length=50,
widget=forms.PasswordInput(),
label="Contraseña"
)
password_confirmation = forms.CharField(
min_length=4,
max_length=50,
widget=forms.PasswordInput(),
label='Confirmar contraseña'
)
first_name = forms.CharField(
min_length=2,
max_length=50,
label='Nombre'
)
last_name = forms.CharField(
min_length=2,
max_length=50,
label='Apellido'
)
email = forms.CharField(
widget=forms.EmailInput(),
label='Email'
)
def clean_username(self):
username = self.cleaned_data['username']
username_taken = User.objects.filter(username=username).exists()
if username_taken:
raise forms.ValidationError('El nombre de usuario ya esta en uso.')
return username
def clean_email(self):
email = self.cleaned_data['email']
email_taken = User.objects.filter(email=email).exists()
if email_taken:
raise forms.ValidationError('El email ya esta en uso.')
return email
def clean(self):
data = super().clean()
password = data['password']
password_confirmation = data['password_confirmation']
if password != password_confirmation:
raise forms.ValidationError('Las contraseñas no coinciden.')
return data
def save(self):
data = self.cleaned_data
data.pop('password_confirmation')
user = User.objects.create_user(**data)
profile = Profile(user=user)
profile.save()
signup.html
<form action="{% url 'users:signup' %}" method="POST">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% for error in field.errors %}
{{ error }}
{% endfor %}
</p>
{% endfor %}
<br>
<button class="register-button" type="submit">Registrarse</button>
</form>
The problem is that the error you raise is not from a specific field and this is because you raise it in the method
clean
, and you only show the field errors :So there are several solutions, one is by indicating the field when raising the exception
ValidationError
:Or you can do it like this (personally, I use this way, it's better, plus you just have to call the method and that's it):
If you don't want to do the above, then you
<form>
can be like this:The "problem" is that the error will be displayed at the top of the form (or you can put it at the bottom).
I hope I've helped.