Skip to content Skip to sidebar Skip to footer

Custom Error Messages Not Working In Django Modelform

I have a ModelForm and I want to customize some of the error messages for required fields. Some of the customized error messages work, but some don't. Here is my code: error_messag

Solution 1:

OK, I found out that I have to define these error messages when I declare the fields or the widgets:

photo = forms.ImageField(required=False, widget=CustomPhotoWidget, label=_('Add profile picture'), error_messages={'required': _("A profile picture is required.")})
diet = forms.ChoiceField(widget=forms.RadioSelect(), label=_('My diet'), error_messages={'required': _("Your diet is required.")})
smoking_status = forms.ChoiceField(widget=forms.RadioSelect(), label=_('My smoking status'), error_messages={'required': _("Your smoking status is required.")})
relationship_status = forms.ChoiceField(widget=forms.RadioSelect(), label=_('My relationship status'), error_messages={'required': _("Your relationship status is required.")})
gender_to_match = forms.MultipleChoiceField(choices=User.GENDER_CHOICES, widget=forms.CheckboxSelectMultiple, error_messages={'required': _("Gender to match is required.")})
# ~~~~ TODO: define all the languages and not just hard-code languages like below._city = forms.CharField(label=_('City or locality'), max_length=120, error_messages={'required': _("Please write where you live.")})
city_en = _city
city_he = _city

https://github.com/speedy-net/speedy-net/blob/staging/speedy/match/accounts/forms.py#L58-L66

The error message can also be customized in the __init__ method:

if (to_attribute(name='profile_description') in self.fields):
        self.fields[to_attribute(name='profile_description')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Please write a few words about yourself.")}
    if (to_attribute(name='city') in self.fields):
        self.fields[to_attribute(name='city')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Please write where you live.")}
    if (to_attribute(name='children') in self.fields):
        self.fields[to_attribute(name='children')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Do you have children? How many?')
        self.fields[to_attribute(name='children')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Do you have children? How many?")}
    if (to_attribute(name='more_children') in self.fields):
        self.fields[to_attribute(name='more_children')].label = pgettext_lazy(context=self.instance.user.get_gender(), message='Do you want (more) children?')
        self.fields[to_attribute(name='more_children')].error_messages = {'required': pgettext_lazy(context=self.instance.user.get_gender(), message="Do you want (more) children?")}
    if (to_attribute(name='match_description') in self.fields):
        self.fields[to_attribute(name='match_description')].label = pgettext_lazy(context=self.instance.get_match_gender(), message='My ideal match')
        self.fields[to_attribute(name='match_description')].error_messages = {'required': pgettext_lazy(context=self.instance.get_match_gender(), message="Who is your ideal partner?")}

https://github.com/speedy-net/speedy-net/blob/staging/speedy/match/accounts/forms.py#L170-L182

Post a Comment for "Custom Error Messages Not Working In Django Modelform"