I'm trying to display an error message for a certain error that user makes while registering, for now I've only covered an error regarding password and conf_passowrd not matching, but only that scenario works, whenever there is an error regarding, for example, too short password entered, it still displays the error regarding password mismatch.
What I've tried so far:
view.py:
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm
def indexsignup(request):
form = UserCreationForm()
if request.method == 'POST':
regForm = UserCreationForm(request.POST)
if regForm.is_valid():
regForm.save()
return redirect('login')
else:
for msg in form.error_messages:
if len('password1') < 6:
messages.error(request, f"PAss short")
print(msg)
else:
messages.error(request, f"Two passwords don't match.")
print(msg)
return render(request, 'registration/register.html', {'form':form})
register.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Forum - SignUp</title>
<link rel="stylesheet" href="{% static 'signup/style.css' %}">
</head>
<body>
<div class="signin-box">
<h1> Sign Up</h1>
<form method="post" action="">
{% csrf_token %}
<div class="textbox">
<input type="text" name="username" placeholder="Username" maxlength="10" autocapitalize="none"
autocomplete="username" autofocus="" required="" id="id_username">
</div>
<div class="textbox">
<input type="password" name="password1" placeholder="Password" autocomplete="new-password" required=""
id="id_password1">
<script type="text/javascript">
function reveal() {
if (document.getElementById('box').checked) { document.getElementById("id_password1").type = 'text'; }
else
document.getElementById("id_password1").type = 'password';
}
</script>
<div class="check">
<input title="Reveal password" type="checkbox" id="box" onclick="reveal()">
</div>
</div>
<div class="textbox">
<input type="password" name="password2" placeholder="Confirm Password" autocomplete="new-password"
required="" id="id_password2">
<script type="text/javascript">
function reveal2() {
if (document.getElementById('box').checked) { document.getElementById("id_password2").type = 'text'; }
else
document.getElementById("id_password2").type = 'password';
}
</script>
<div class="check">
<input title="Reveal password" type="checkbox" id="box" onclick="reveal2()">
</div>
</div>
{% if messages %}
{% for message in messages %}
<div class="help">
<p><span style="font-size: 20px">⚠</span> {{message}}</p>
</div>
{% endfor %}
{% endif %}
<input class="btn" type="submit" style="opacity: 1 !important;" value="Sign Up">
</form>
</div>
</body>