开发者

displaying django form error messages instead of just the field name

开发者 https://www.devze.com 2023-04-08 03:35 出处:网络
I have a form and I want to display the errors in a for lo开发者_Go百科op. {% for error in form.errors %}

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

精彩评论

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