开发者

displaying django form error messages instead of just the field name

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜