自定义错误消息在Django ModelForm中不起作用 [英] Custom error messages not working in Django ModelForm

查看:96
本文介绍了自定义错误消息在Django ModelForm中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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_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:不起作用。

I marked the custom error messages which are not working with # ~~~~ TODO: not working.. The others are working.

有什么建议吗?

推荐答案

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

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

错误消息也可以在 __ init __ 方法:

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

这篇关于自定义错误消息在Django ModelForm中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆