Django override default form error messages
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)
Thanks!
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)
....
Hope this helps.
To globally override the "required" error message, set the default_error_messages class attribute on Field:
# form error message override
from django.forms import Field
from django.utils.translation import ugettext_lazy
Field.default_error_messages = {
'required': ugettext_lazy("This field is mandatory."),
}
This needs to happen before any fields are instantiated, e.g. by including it in settings.py.