我有一个ModelForm,我想为必填字段自定义一些错误消息。一些自定义的错误消息有效,但有些无效。这是我的代码:

error_messages = {
    'photo': {
        'required': _("A profile picture is required."),
    },
    'height': {
        'required': _("Your height is required."),
    },
    'diet': {
        'required': _("Your diet is required."),  # ~~~~ TODO: not working.
    },
    'smoking_status': {
        'required': _("Your smoking status is required."),  # ~~~~ TODO: not working.
    },
    'relationship_status': {
        'required': _("Your relationship status is required."),  # ~~~~ TODO: not working.
    },
    **{to_attribute(name='profile_description', language_code=language_code): {
        'required': _("Please write a few words about yourself."),
    } for language_code, language_name in django_settings.LANGUAGES},
    **{to_attribute(name='city', language_code=language_code): {
        'required': _("Please write where you live."),  # ~~~~ TODO: not working.
    } for language_code, language_name in django_settings.LANGUAGES},
    **{to_attribute(name='children', language_code=language_code): {
        'required': _("Do you have children? How many?"),
    } for language_code, language_name in django_settings.LANGUAGES},
    **{to_attribute(name='more_children', language_code=language_code): {
        'required': _("Do you want (more) children?"),
    } for language_code, language_name in django_settings.LANGUAGES},
    **{to_attribute(name='match_description', language_code=language_code): {
        'required': _("Who is your ideal partner?"),
    } for language_code, language_name in django_settings.LANGUAGES},
    'gender_to_match': {
        'required': _("Gender to match is required."),  # ~~~~ TODO: not working.
    },
    'min_age_to_match': {
        'required': _("Minimal age to match is required."),
    },
    'max_age_to_match': {
        'required': _("Maximal age to match is required."),
    },
    'diet_match': {
        'required': _("Diet match is required."),
    },
    'smoking_status_match': {
        'required': _("Smoking status match is required."),
    },
    'relationship_status_match': {
        'required': _("Relationship status match is required."),
    },
}


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

我标记了不能与# ~~~~ TODO: not working.一起使用的自定义错误消息。其他人在工作。

有什么建议么?

最佳答案

好的,我发现在声明字段或窗口小部件时必须定义以下错误消息:

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

错误消息也可以在__init__方法中自定义:

    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

09-25 20:09