Opened 10 years ago

Closed 10 years ago

#22636 closed Cleanup/optimization (invalid)

NON_FIELD_ERRORS should be accessible via Form.errors.as_data() in a template

Reported by: sebastian.annies@… Owned by: nobody
Component: Forms Version: 1.7-beta-2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

A naive developer would try to access the non-field errors in a template like that:

{% for messages in form.errors.as_data.__all__ %}
    {% for message in messages %}
            <div class="alert alert-error">{{ message }}</div>
    {% endfor %}
{% endfor %}

Unfortunately this is not possible as

Variables and attributes may not begin with underscores: 'form.errors.as_data.__all__'

The key should not start with underscore to make the data accessible in templates

Change History (1)

comment:1 by Matthias Erll, 10 years ago

Resolution: invalid
Status: newclosed

The behavior is intentional, and it should not be necessary to access this field in a template directly. In order to render non-field-errors in templates, use non_field_errors() instead; alternatively iterating over all errors in the errors attribute is still possible. The new methods as_data() and as_json() are more useful for other output formats such as JSON responses.

Note: See TracTickets for help on using tickets.
Back to Top