如何在Django自定义注册模板中显示错误信息
Hey there! Let's fix that issue where your custom signup template isn't showing error messages. I spotted two key problems in your code—let's walk through them step by step:
views.py) Right now, when your form fails validation (like when passwords don't match or a username is taken), you're creating a brand new empty form instead of passing the invalid one (which contains all the error data) to your template. That's why you never see errors—your template only gets a fresh, error-free form every time validation fails.
Here's the fixed views.py code:
def signup(request): if request.method == 'POST': form = SignupForm(request.POST) if form.is_valid(): form.save() return redirect(settings.LOGIN_URL) # Remove the else clause here! We want to pass the invalid form to the template below else: form = SignupForm() return render(request, 'accounts/register.html', {'form': form})
Your current template only handles non_field_errors (like password mismatch errors that don't belong to a single input), but you're not displaying errors for individual fields (like "username already exists" or "password too short"). Also, your manual input tags aren't linked to the form's field data, so they can't show error states or retain user input.
Option 1: Use Django's form field rendering (cleaner, keeps form functionality)
This lets Django handle input attributes while you keep your custom structure:
<div class="text-center mb-4"> <h1 class="h3 mb-3 font-weight-normal">Join Modvisor</h1> <!-- Show non-field errors (e.g., password mismatch) --> {% if form.non_field_errors %} <p class="error-message text-danger"> {% for error in form.non_field_errors %} {{ error }} {% endfor %} </p> {% endif %} </div> <!-- Username/Email Field --> <div class="form-label-group"> {{ form.username }} <label for="{{ form.username.id_for_label }}">Email</label> <!-- Show field-specific errors --> {% if form.username.errors %} <small class="text-danger d-block mt-1"> {% for error in form.username.errors %} {{ error }} {% endfor %} </small> {% endif %} </div> <!-- Password 1 Field --> <div class="form-label-group"> {{ form.password1 }} <label for="{{ form.password1.id_for_label }}">Password</label> {% if form.password1.errors %} <small class="text-danger d-block mt-1"> {% for error in form.password1.errors %} {{ error }} {% endfor %} </small> {% endif %} </div> <!-- Password 2 Field --> <div class="form-label-group"> {{ form.password2 }} <label for="{{ form.password2.id_for_label }}">Confirm Password</label> {% if form.password2.errors %} <small class="text-danger d-block mt-1"> {% for error in form.password2.errors %} {{ error }} {% endfor %} </small> {% endif %} </div>
Option 2: Keep manual input tags (if you want full control over HTML)
If you prefer writing your own inputs, make sure to add error rendering and retain user input:
<div class="form-label-group"> <input type="text" id="inputEmail" class="form-control {% if form.username.errors %}is-invalid{% endif %}" name="username" placeholder="Email address" required autofocus value="{{ form.username.value|default:'' }}"> <label for="inputEmail">Email</label> {% if form.username.errors %} <div class="invalid-feedback"> {% for error in form.username.errors %} {{ error }} {% endfor %} </div> {% endif %} </div>
Add Bootstrap classes like text-danger or invalid-feedback to make error messages look polished and match your existing form style.
Once you fix both issues, try triggering a form error (like using a duplicate username or mismatched passwords) and you'll see the error messages show up as expected!
内容的提问来源于stack exchange,提问作者Jay P.

