[Fixed]-Displaying Django error messages in templates? – Not working

1👍

non_field_errors(), as their name implies, show errors not associated with a field (docs).

Note that any errors raised by your Form.clean() override will not be associated with any field in particular. They go into a special “field” (called all), which you can access via the non_field_errors() method if you need to.

You shouldn’t be using it as logic for whether a specific field has an error. You should be doing this:

{% if not form.email.errors %}
    <div class="required field">
      <label>Emailaddress</label>
      {{ form.email }}
    </div>
{% endif %}

and then in your login.html, you’re not rendering {{ field.errors }} like you do in register.html:

{% if form.non_field_errors %}
    <div class="required field error">
      <label>Emailaddress</label>
        {{ form.username }}

      {# This is missing #}
      <p>{{ form.username.errors }}</p>    

    </div>
{% endif %}

Leave a comment