17

At my template, I want to iterate through all form errors, including the ones that are NOT belonging to a specific field. ( which means for form.errors, it should also display for __all__ errors aswell)

I have tried several versions, Ie:

 <div id="msg">
  {% if form.errors %}
  <div class="error">
   <p><span>ERROR</span></p>
   <ul>
   {% for key,value in form.errors %}
    {% for error in value %}
     <li>{{ error }}</li>
    {% endfor %}
   {% endfor %}
   </ul>
  </div>
  {% endif %}
 </div>

Still no achievement, I will be greatful for any suggestion.

2 Answers 2

43

Form errors in Django are implemented as an ErrorDict instance (which is just a subclass of dict with extras). Try a slight adjustment to your template for loop syntax:

{% for key, value in form.errors.items %}
19

Are you, by any chance, looking for form.non_field_errors? That is how you would get access to the errors that aren't associated with a particular field.

{% if form.non_field_errors %}
<ul>
    {{ form.non_field_errors.as_ul }}
</ul>
{% endif %}

Check the forms.py test suite as well for another example. Search for form.non_field_errors

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.