Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling django non-field errors on forms

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.

like image 776
dfrankow Avatar asked Sep 02 '09 15:09

dfrankow


2 Answers

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 %}
like image 123
rukeba Avatar answered Nov 02 '22 19:11

rukeba


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

like image 41
Alex Gaynor Avatar answered Nov 02 '22 20:11

Alex Gaynor