当我尝试从表单中打印错误时得到非常奇怪的输出。这是我在模板中的代码(如果您对 CSS 感到好奇,请使用 Bootstrap 3),
{% if not form.is_valid and form.is_bound %}
<div class="row">
<div class="col col-lg-10 col-offset-1 text-center">
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
{% for field in form %}
{% for error in field.errors %}
<strong>{{ field.label }}</strong>: {{ error }} <br>
{% endfor %}
{% endfor %}
</div>
</div>
</div>
{% endif %}
所有其他错误都很好,除非我输入了错误的密码。然后,我得到这样的输出,
Password: I
Password: n
Password: c
Password: o
Password: r
Password: r
Password: e
Password: c
Password: t
Password:
Password: p
Password: a
Password: s
Password: s
Password: w
Password: o
Password: r
Password: d
Password: ,
Password:
Password: p
Password: l
Password: e
Password: a
Password: s
Password: e
Password:
Password: t
Password: r
Password: y
Password:
Password: a
Password: g
Password: a
Password: i
Password: n
Password: .
仔细一看,是一句“密码错误,请重试”。除了每个角色都有自己的台词。
这里发生了什么?
为了进一步参考,这是我在视图中分配错误消息的方式,
form.errors['email'] = 'Email not found, please try again.'
form.errors['password'] = 'Incorrect password, please try again.'
非常感谢!!