显示django表单错误消息,而不仅仅是字段名称 [英] displaying django form error messages instead of just the field name
本文介绍了显示django表单错误消息,而不仅仅是字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单,我想在for循环中显示错误。
I have a form and I want to display the errors in a for loop.
{% for error in form.errors %}
<tr><td>{{ error }}</td></tr>
{% endfor %}
通过这样做,{{error}}只包含具有错误的字段名称,但不是错误消息。如何显示错误消息?
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 %}
或对于特定字段:
{% if form.subject.errors %}
<ol>
{% for error in form.subject.errors %}
<li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}
更多信息在这里: https://docs.djangoproject.com/en/dev/topics/forms/#customizing -the-form-template
这篇关于显示django表单错误消息,而不仅仅是字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文