【发布时间】:2017-06-02 08:12:36
【问题描述】:
我需要提取消息和字段。
例如,我有这个django表单错误结果
<ul class="errorlist">
<li>__all__
<ul class="errorlist nonfield">
<li>Pointofsale with this Official receipt and Company already exists.</li>
</ul>
</li>
</ul>
来自此代码的输出
def post_sale(request):
sale_form = request["data"]
if sale_form.is_valid():
save_form.save()
else:
print save_form.errors
但我需要实现的是获取没有标签的消息,所以我可以以纯字符串/文本的形式返回这些消息。
def post_sale(request):
sale_form = request["data"]
if sale_form.is_valid():
save_form.save()
else:
# This is just pseudo code
for field in save_form.errors:
field = str(field["field"})
message = str(field["error_message"])
print "Sale Error Detail"
print field
print message
error_message = { 'field':field,'message':message }
error_messages.append(error_message )
输出将是:
Sale Error Detail
(the field where form error exists)
Pointofsale with this Official receipt and Company already exists.
探索的问题和文档
- displaying django form error messages instead of just the field name
- Getting a list of errors in a Django form
- django form errors. get the error without any html tags
- How do I display the Django '__all__' form errors in the template?
- https://docs.djangoproject.com/en/1.10/ref/forms/api/
- https://docs.djangoproject.com/en/1.10/topics/forms/
谢谢,请告诉我是否有问题或需要澄清,以便我修复它。
【问题讨论】:
标签: django django-forms