40π
I think the problem here is that formset.errors
is a list of dictionaries, not a single dictionary.
From the Django docs page on formsets:
>>> formset.errors
[{}, {'pub_date': [u'This field is required.']}]
See if something like this fixes the problem: (Updated based on the askers comments)
{% for dict in formset.errors %}
{% for error in dict.values %}
{{ error }}
{% endfor %}
{% endfor %}
If that fails, Iβd try using manage.py shell
, and try to reproduce your situation in the python shell⦠that way it will be easy to inspect the various values and figure out what you need to do.
13π
The for loops are unnecessary, these errors should be correctly displayed with the following:
{{ formset.non_form_errors }}
- [Django]-Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?
- [Django]-Put a <a> hyperlink in a django message
- [Django]-The request's session was deleted before the request completed. The user may have logged out in a concurrent request, for example
7π
Here is a clarification for anyone encountering similar issues of errors not being rendered in template:
If you have and error regarding the formset as a whole, use:
{{ formset.non_form_errors }}
this basically returns errors in __all__
entry from formset.errors
. It is documented as:
"""
Returns an ErrorList of errors that aren't associated with a particular
form -- i.e., from formset.clean(). Returns an empty ErrorList if there
are none.
"""
However if you are rendering forms from formset and some errors are not being renderd, you are probably missing:
{% for form in formset.forms %}
{# ... #}
{{ form.non_field_errors }}
{% endfor %}
this returns errors in __all__
entry from form.errors
. Those are, analogous to the non_form_errors
, the errors that arenβt associated with a particular field, but rather with the field relations. For example if you had a form with fields From
and To
, and you validate if From
value is smaller then To
value, the tag {{ form.non_field_errors }}
could render the following error:
'The From value must be smaller than the To value'
- [Django]-Django SUM Query?
- [Django]-Django model one foreign key to many tables
- [Django]-How can I disable logging while running unit tests in Python Django?
4π
Django 1.6 formsets have a new method, BaseFormSet.total_error_count
. Using this in a template conditional ensures you only will output errors and markup if at least one error exists in the formset.
{% if formset.total_error_count %}
<ul class="errorList">
{% for dict in formset.errors %}
{% for error in dict.values %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endif %}
See the Django docs page for v1.6+.
- [Django]-How to get the current language in Django?
- [Django]-How to compare two JSON objects with the same elements in a different order equal?
- [Django]-Django admin sort foreign key field list