[Fixed]-Styling django non-field errors on forms

28👍

I use this template to style non fields errors:

{% if form.non_field_errors %}
  <div class="non-field-errors">
    {% for err in form.non_field_errors %}
      <p class="form-error">{{ err }}</p>
    {% endfor %}
  </div>
{% endif %}
👤rukeba

10👍

In your template you can access {{ form.non_field_errors }} and render them however you like. Here’s how the Django admin handles them for example: http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templates/admin/change_form.html#L40

8👍

Options:

Both {{ form.non_field_errors }} and {{ form.non_field_errors.as_ul }} do the same thing.

{{ form.non_field_errors.as_text }} displays the errors with an * in front of the text.

This article is also helpful in explaining why the * will not be removed django ticket

0👍

Can be done without complicating the template language. In Django 3 the non_field_errors list has classes "errorlist nonfield", so you can simply use CSS to style them:

In your CSS file:

.errorlist.nonfield {
    color: red;
}

In your html template:

{{ form.as_p }}

Leave a comment