i have a problem. I'm trying to catch errors from password validation in Django, but I don't know how to do it. Password validation is called in forms.py, and form.isValid() is called in views.py I'd like to know exactly what errors has been found in the password validation called in forms.py, so i can do use an if to launch a bootstrap warning chosen by me. Here is the code:
forms.py
class RegistrationForm(forms.Form):
username = forms.CharField(label="Inserisci username", max_length=50)
password = forms.CharField(label="Inserisci password", widget=forms.PasswordInput)
repeatPassword = forms.CharField(label="Ripeti password", widget=forms.PasswordInput)
def clean_username(self):
username = self.cleaned_data['username']
if User.objects.filter(username=username).exists():
raise forms.ValidationError('Nome utente già in uso')
return username
def clean_password(self):
username = self.cleaned_data['username']
password = self.cleaned_data['password']
validate_password(password, username)
return password
def clean_password2(self):
password = self.cleaned_data['password']
repeatPassword = self.cleaned_data['repeatPassword']
if password != repeatPassword:
raise forms.ValidationError('Le password devono corrispondere')
return password
views.py
def registration(request):
if request.method == "POST":
registrationForm = RegistrationForm(request.POST)
if registrationForm.is_valid():
# Creazione utente
username = RegistrationForm.cleaned_data['username']
password = RegistrationForm.cleaned_data['password']
playerStatistics = Statistics(health=randint(90, 110), armor=0, strength=randint(5, 15),
speed=randint(15, 25), dexterity=randint(15, 25))
equipmentStatistics = Statistics(health=0, armor=0, strength=0, speed=0, dexterity=0)
playerStatistics.save()
equipmentStatistics.save()
equipment = Equipment(statistics=equipmentStatistics)
equipment.save()
inventory = Inventory()
inventory.save()
newPlayer = Player(username=username, password=password, statistics=playerStatistics,
equipment=equipment, inventory=inventory)
newPlayer.save()
return render(request, "playerHome.html")
else:
return HttpResponse("Errore nell'inserimento dei dati")
else:
registrationForm = RegistrationForm()
return render(request, "registration.html", {"form": registrationForm})
registration.html form
<form action="/registration/" method="post">
{% csrf_token %} {{ form|crispy }}
{% if samePasswordAlert %}
<div class="alert alert-danger" role="alert">Le password non corrispondono</div>
{% endif %}
{% if existingUserAlert %}
<div class="alert alert-danger" role="alert">Il nome utente è già in uso</div>
{% endif %}
{% if tooShortPassword %}
<div class="alert alert-danger" role="alert">La password deve contenere almeno 8 caratteri</div>
{% endif %}
{% if tooSimilarToUserPassword %}
<div class="alert alert-danger" role="alert">La password è troppo simile al nome utente</div>
{% endif %}
{% if tooCommonPassword %}
<div class="alert alert-danger" role="alert">La password è troppo comune</div>
{% endif %}
{% if entirelyNumericPassword %}
<div class="alert alert-danger" role="alert">La password non può contenere solo numeri</div>
{% endif %}
<button type="submit" class="btn btn-primary">Registrati</button>
</form>
Thanks
I tried to use form.errors, but I don't understand exactly how to use it, so it gives me an error.