Django会覆盖默认表单错误消息 [英] Django override default form error messages
本文介绍了Django会覆盖默认表单错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何覆盖项目中所有应用程序(或至少为1个应用程序)的默认表单错误消息(例如:需要其他语言)
How can I overwrite the default form error messages (for example: need them in other language) for the all apps in my project (or at least for 1 app)
谢谢!
推荐答案
最简单的方法是为表单字段定义提供一组默认错误。表单域可以为其指定参数。例如:
The easiest way is to provide your set of default errors to the form field definition. Form fields can take a named argument for it. For example:
my_default_errors = {
'required': 'This field is required',
'invalid': 'Enter a valid value'
}
class MyForm(forms.Form):
some_field = forms.CharField(error_messages=my_default_errors)
....
希望这有帮助。
这篇关于Django会覆盖默认表单错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文