在视图*问表单字段“错误”

时间:2022-05-31 14:58:38

I want to know in my view, if a certain field raised an error, that way I can send some values to the template (index of current tab (using a javascript tab widget)). Is it possible?

我想在我的视图中知道,如果某个字段引发了错误,那么我可以向模板发送一些值(当前选项卡的索引(使用javascript选项卡小部件))。可能吗?

Thanks in advance.

提前致谢。

1 个解决方案

#1


15  

You can definitely access your form errors in your view. Each bound Form instance has an errors attribute that gives you a dictionary mapping field name to error-message lists:

您绝对可以在视图中访问表单错误。每个绑定的Form实例都有一个errors属性,该属性为您提供字典映射字段名称到错误消息列表:

>>> f = ContactForm({'subject': 'Hello', 'message':''})
>>> f.errors
{'message':[u'This field is required.']}

You can access individual fields as follows:

您可以按如下方式访问各个字段:

>>> if f['subject'].errors:
        values = [Add values to send to template]

#1


15  

You can definitely access your form errors in your view. Each bound Form instance has an errors attribute that gives you a dictionary mapping field name to error-message lists:

您绝对可以在视图中访问表单错误。每个绑定的Form实例都有一个errors属性,该属性为您提供字典映射字段名称到错误消息列表:

>>> f = ContactForm({'subject': 'Hello', 'message':''})
>>> f.errors
{'message':[u'This field is required.']}

You can access individual fields as follows:

您可以按如下方式访问各个字段:

>>> if f['subject'].errors:
        values = [Add values to send to template]