Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django form errors prints __all__

This code in the default login template:

{{ form.errors }}

Produces this html output when the account is inactive:

<ul class="errorlist">
  <li>__all__
    <ul class="errorlist">
      <li>This account is inactive.</li>
    </ul>
  </li>
</ul>

Why does it print the string _all_?

I'm using the development version by the way.

like image 731
Asdf Avatar asked Mar 07 '11 14:03

Asdf


People also ask

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What does cleaned_data do in Django?

cleaned_data is where all validated fields are stored.

What method can you use to check if form data has changed when using a form instance?

Use the has_changed() method on your Form when you need to check if the form data has been changed from the initial data. has_changed() will be True if the data from request.

What is form AS_P in Django?

{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.


2 Answers

Ah, I should have used:

{{ form.non_field_errors }}

instead

like image 109
Asdf Avatar answered Sep 24 '22 02:09

Asdf


If you, like me, still want to display ALL errors at once, you can loop over form.errors.items.

This line:

{{ form.errors }}

Becomes this: (or similar)

<ul class="errorlist">
    {% for key, value in form.errors.items %}
    <li>{% if key != '__all__' %}{{ key }} {% endif %}{{ value }}</li>
    {% endfor %}
</ul>
like image 31
graup Avatar answered Sep 22 '22 02:09

graup