gpt4 book ai didi

python - 在View中访问Django Form报错信息

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:20 25 4
gpt4 key购买 nike

在我看来,我可以访问 form['item'].errors,它给了我类似的东西:

> form.is_valid()
False
>
> e = form['name'].errors
>
> print e
<ul class="errorlist"><li>There already exists an item with this name. Try a different one.</li></ul>
>
> e.as_text()
* name\n * There already exists an item with this name. Try a different one.

但是,如何在不显示 HTML 标记或 *name\n * 的情况下访问 There already exists... 错误消息?

最佳答案

我相信您正在寻找 as_data()

对于整个表单:

print(form.errors.as_data())

{'foo': [ValidationError([u'This is an error.'])], 'bar': [ValidationError([u'This is another error.'])]}

对于一个字段:

for e in form.errors['foo'].as_data():
print e

[u'This field is required.']

关于python - 在View中访问Django Form报错信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33271560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com