django文档说https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.errors https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.errors
访问错误属性以获取错误消息字典:
f.错误
{'sender': ['输入有效的电子邮件地址。'], 'subject': ['此字段为必填项。']}
但是,当我尝试打印 forms.errors 时,我得到了 html 代码。
def login(request):
if request.method == "GET":
return render(request, r"login.html")
else:
form = LoginForm(request.POST)
if form.is_valid():
return HttpResponse("valid")
else:
print form.errors
return HttpResponse("Invalid")
Django version 1.6.4, using settings 'test_proj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
<ul class="errorlist"><li>username<ul class="errorlist"><li>Ensure this value ha
s at most 3 characters (it has 6).</li></ul></li></ul>
我需要获取原始错误来显示 ajax 消息,例如
response_json = {"status": "error", "content": "Username is too short"}
如果还有其他方法获取原始错误消息,也可以告诉我。谢谢!
这只是调用的一个产物print
:隐式转换为字符串,以及错误列表类__str__ method https://github.com/django/django/blob/master/django/forms/utils.py#L79称其为as_ul
输出为 HTML 的方法 - 这样您就可以通过执行以下操作在模板中输出错误列表{{ form.errors }}
.
对象本身的内容不是 HTML 格式的,您可以通过以下方式查看它print repr(form.errors)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)