88👍
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)
....
8👍
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.
- [Django]-Django Admin nested inline
- [Django]-How to concatenate strings in django templates?
- [Django]-Find object in list that has attribute equal to some value (that meets any condition)
7👍
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",
)
....
- [Django]-Difference between objects.create() and object.save() in django orm
- [Django]-How to resolve "django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: foo" in Django 1.7?
- [Django]-Where's my JSON data in my incoming Django request?
- [Django]-How do I get user IP address in Django?
- [Django]-How do I check for last loop iteration in Django template?
- [Django]-Django – Login with Email
3👍
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!
- [Django]-How can I use Django permissions without defining a content type or model?
- [Django]-Can't install via pip because of egg_info error
- [Django]-Python/Django: log to console under runserver, log to file under Apache
3👍
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.
- [Django]-Handling race condition in model.save()
- [Django]-How can I use Django permissions without defining a content type or model?
- [Django]-Using {% url ??? %} in django templates
3👍
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
- [Django]-How to Unit test with different settings in Django?
- [Django]-How do I print out the contents of my settings in a django shell?
- [Django]-Default filter in Django admin
-2👍
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.')
- [Django]-What is choice_set in this Django app tutorial?
- [Django]-Location of Django logs and errors
- [Django]-Django Rest Framework with ChoiceField
-6👍
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/
- [Django]-Customize/remove Django select box blank option
- [Django]-Choose test database?
- [Django]-Pass extra arguments to Serializer Class in Django Rest Framework