Reputation: 21407
If I look in Django's forms.py, as_p()
calls _html_output()
, which styles field errors with self.error_class()
(although I can't locate the definition of that).
However, _html_output()
does NOT style non_field_errors
(a.k.a. top_errors in the code).
How does one style the non-field errors? Cut and paste all of _html_output?
I am using Django 1.0.
Upvotes: 17
Views: 32811
Reputation: 1241
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 }}
Upvotes: 0
Reputation: 691
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 %}
Upvotes: 29
Reputation: 10249
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
Upvotes: 9
Reputation: 15019
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
Upvotes: 10