Django循环所有窗体错误 [英] Django, loop over all form errors
本文介绍了Django循环所有窗体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的模板中,我想迭代所有的表单错误,包括不属于特定字段的错误。 (这意味着对于form.errors,它也应该显示 __ all __
错误aswell)
At my template, I want to iterate through all form errors, including the ones that are NOT belonging to a specific field. ( which means for form.errors, it should also display for __all__
errors aswell)
我已经尝试了几个版本,即:
I have tried several versions, Ie:
<div id="msg">
{% if form.errors %}
<div class="error">
<p><span>ERROR</span></p>
<ul>
{% for key,value in form.errors %}
{% for error in value %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
</div>
仍然没有成就,我会很乐意为任何建议。
Still no achievement, I will be greatful for any suggestion.
推荐答案
Django中的表单错误被实现为一个 ErrorDict
实例(这只是一个 dict
与附加)。尝试稍微调整您的模板循环语法:
Form errors in Django are implemented as an ErrorDict
instance (which is just a subclass of dict
with extras). Try a slight adjustment to your template for loop syntax:
{% for key, value in form.errors.items %}
这篇关于Django循环所有窗体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文