· 

Django: Password error

1.  A Problem

This is part of AdminSignupView():

        # 🆕 First-time signup

        else:

            form = AdminSignupForm(request.POST)

            if not form.is_valid():

                print("form.is_valid() is not True")

                return render(request, "accounts/signup.html", {"form": form})

            print("Creating new user") # debug

            user = form.save(commit=False)

            user.is_active = False

            user.has_paid = False

            user.signup_completed = False

            user.save()

With this, if form.is_valid() is not true, the signup form is displayed silently again.  I tried to signup with a password of 6 letters and numbers, which seems invalid.  I would like to show an error message on the signup form display.  How can I do it?

 

Django already stores the validation errors inside the form when form.is_valid() fails. You only need to display those errors in the template. You do not need extra logic in the view.

Your view is already correct:

if not form.is_valid():
print("form.is_valid() is not True")
return render(request, "accounts/signup.html", {"form": form})

Because you pass the same form object back to the template, it contains:

  • field errors

  • password validation errors

  • non-field errors

The only missing step is showing them in the template.


1️⃣ The simplest way (recommended)

In your template:

accounts/templates/accounts/signup.html

Add this near the top of the form:

{% if form.errors %}
<div class="errorlist">
Please correct the errors below.
</div>
{% endif %}
{{ form.as_p }}

Django will automatically show errors like:

This password is too short. It must contain at least 8 characters.

2️⃣ Better display (more professional)

Instead of form.as_p, render fields manually so errors appear under each field.

Example:

<form method="post">
{% csrf_token %}
<div>
{{ form.username.label_tag }}
{{ form.username }}
{{ form.username.errors }}
</div>
<div>
{{ form.email.label_tag }}
{{ form.email }}
{{ form.email.errors }}
</div>
<div>
{{ form.password1.label_tag }}
{{ form.password1 }}
{{ form.password1.errors }}
</div>
<div>
{{ form.password2.label_tag }}
{{ form.password2 }}
{{ form.password2.errors }}
</div>
<button type="submit">Sign Up</button>
</form>

Then the error will appear like:

Password:
[ input box ]

• This password is too short. It must contain at least 8 characters.

3️⃣ Very useful for debugging

You can also temporarily print all errors:

<pre>{{ form.errors }}</pre>

Example output:

{'password1': ['This password is too short.']}

4️⃣ Why Django already knows the password is invalid

Your form probably inherits from:

django.contrib.auth.forms.UserCreationForm

That automatically applies validators defined in:

AUTH_PASSWORD_VALIDATORS

in settings.py.

Example:

AUTH_PASSWORD_VALIDATORS = [
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]

So your 6-character password failed the MinimumLengthValidator.


5️⃣ Small improvement to your view

You can simplify:

if not form.is_valid():
return render(request, "accounts/signup.html", {"form": form})

to:

if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.has_paid = False
user.signup_completed = False
user.save()
else:
return render(request, "accounts/signup.html", {"form": form})

Or even simpler.


🎯 Final Result

When the user submits a weak password like:

abc123

The page will reload and show:

This password is too short. It must contain at least 8 characters.

without silently failing.


 

✅ If you want, I can also show you a very clean SaaS-style signup form template (similar to Stripe/GitHub style) that displays Django errors beautifully. It would fit your commercial client portal very well.