1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from django import template
from django import forms
from django.forms.forms import NON_FIELD_ERRORS
from django.forms.util import ErrorDict
register = template.Library()
@register.filter
def nice_errors(form, non_field_msg='General form errors'):
nice_errors = ErrorDict()
if isinstance(form, forms.BaseForm):
for field, errors in form.errors.items():
if field == NON_FIELD_ERRORS:
key = non_field_msg
else:
key = form.fields[field].label
nice_errors[key] = errors
return nice_errors
|
More like this
- partition template filters by SmileyChris 5 years, 8 months ago
- "an" filter by SmileyChris 4 years ago
- Gzip decorator by SmileyChris 4 years, 10 months ago
- Log in a user without requiring credentials by SmileyChris 3 years, 11 months ago
- Customizable newforms labels with a template tag by exogen 5 years, 1 month ago
Comments
Example?
#
Example:
(My line-formatting is going to get wiped out by the Markdown processor, so you'll have to paste this into an editor and do indenting there.)
{% if form.errors %} <ul class="messages"> {% with form|nice_errors as qq %} {% for error_name,desc in qq.items %} <li>{{ error_name }}:</li> <li>{{ desc }}</li> {% endfor %} </ul> {% endwith %} {% endif %}Since ErrorDict does some auto-HTMLizing of the dict values, this will produce doubly-nested unordered lists.
#