Django override default form error messages
DjangoFormsMessageDjango Problem Overview
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!
Django Solutions
Solution 1 - Django
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.
Solution 2 - Django
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.
Solution 3 - Django
Also come here from google and what I need is to overwrite the default required messages for all fields in my form rather than passing the error_messages argument everytime I defined new form fields. Also, I'm not ready yet to delve into i18n, this apps not required to be multilingual. The comment in this blog post is the closest to what I want:-
http://davedash.com/2008/11/28/custom-error-messages-for-django-forms/
For all form fields that has required messages, this is what I did:-
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for k, field in self.fields.items():
if 'required' in field.error_messages:
field.error_messages['required'] = 'You have to field this.'
class MUserForm(MyForm):
user = forms.CharField(
label="Username",
)
....
Solution 4 - Django
You may want to look at Django's excellent i18n support.
Solution 5 - Django
Hmm, seems there is no easy workaround for the problem.
While skimming through the Django code, I've found that default error messages are hard-coded into each form field class, for ex:
class CharField(Field):
default_error_messages = {
'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'),
'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'),
}
And the easiest way is to use the error_messages
argument, so I had to write the wrapper function:
def DZForm(name, args = {}):
error_messages = {
'required': u'required',
'invalid': u'invalid',
}
if 'error_messages' in args.keys():
args['error_messages'] = error_messages.update(args['error_messages'])
else:
args['error_messages'] = error_messages
return getattr(forms, name)(**args)
If someone knows a more elegant way of doing this would really appreciate seeing it :)
Thanks!
Solution 6 - Django
Say I have a BaseForm
with some error_messages
dictionary like:
error_messages = {
'required': 'This field is required',
'caps': 'This field if case sensitive'
}
and I want to override one of the error messages:
class MySpecialForm(BaseForm):
def __init__(self, *args, **kwargs):
super(MySpecialForm, self).__init__(*args, **kwargs)
self.error_messages['caps'] = 'Hey, that CAPSLOCK is on!!!'
Basically, just override one of the dictionary values. I am not sure how it would work with internationalization though.
Solution 7 - Django
from ProDjango book:
from django.forms import fields, util
class LatitudeField(fields.DecimalField):
default_error_messages = {
'out_of_range': u'Value must be within -90 and 90.',
}
def clean(self, value):
value = super(LatitudeField, self).clean(value)
if not -90 <= value <= 90:
raise util.ValidationError(self.error_messages['out_of_range'])
return value
Solution 8 - Django
from django import forms
from django.utils.translation import gettext as _
class MyForm(forms.Form):
# create form field
subject = forms.CharField(required=True)
# override one specific error message and leave the others unchanged
# use gettext for translation
subject.error_messages['required'] = _('Please enter a subject below.')
Solution 9 - Django
Since this page comes up in a search, perhaps it's worth adding my $0.02 even though the question is old. (I'm still getting used to Stack Overflow's particular etiquette.)
The underscore ("_") is an alias (if that's the right term) for ugettext_lazy; just look at the import statements at the top of the file with "hard-coded" messages. Then, Django's internationalization docs should help, e.g. http://www.djangobook.com/en/2.0/chapter19/