Opened 11 years ago
Closed 11 years ago
#22636 closed Cleanup/optimization (invalid)
NON_FIELD_ERRORS should be accessible via Form.errors.as_data() in a template
| Reported by: | 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
  Note:
 See   TracTickets
 for help on using tickets.
    
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 theerrorsattribute is still possible. The new methodsas_data()andas_json()are more useful for other output formats such as JSON responses.