Django 表单错误.得到没有任何 html 标签的错误 [英] django form errors. get the error without any html tags
问题描述
我想在我的模板上获取非 html 版本中表单的错误.
I would like to get on my template the errors of the form in non-html version.
默认情况下,错误由我想避免的
结束.
by default the error is wrap up by <ul class="errorlist">
which I want to avoid.
无论如何要在没有大量代码更改的情况下做到这一点?
Anyway to do it without a massive code change ?
推荐答案
表单文档部分中的此部分包含所有详细信息,即以下代码段:
This section in the forms documentation section has all the details, namely this snippet:
{% if form.subject.errors %}
<ol>
{% for error in form.subject.errors %}
<li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}
将 form
替换为您在模板中调用的表单类.在这个例子中,subject
是表单中的一个字段.如果您想有一个单独的部分来总结所有错误,只需遍历字段即可:
Replace form
with whatever you call your form class in the template. In this example, subject
is a field in the form. If you want to have a separate section to summarize all the errors, simply loop through the fields:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
{{ error|escape }}
{% endfor %}
{% endfor %}
{% endif %}
这篇关于Django 表单错误.得到没有任何 html 标签的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!