I have a form and I want to display the errors in a for lo开发者_Go百科op.
{% for error in form.errors %}
<tr><td>{{ error }}</td></tr>
{% endfor %}
By doing this the {{ error }} only contains the field name that has an error, but not the error message. How can I display the error message?
You can get all field errors in a form like this:
{% for field in form %}
{{ field.errors|striptags }}
{% endfor %}
Or for a specific field:
{% if form.subject.errors %}
<ol>
{% for error in form.subject.errors %}
<li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}
More Infos here: https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template
精彩评论