在 django 中的 EmailField 验证中例外

发布于 2024-10-17 02:29:32 字数 595 浏览 1 评论 0原文

我有一个名为 EmailHistory 的模型,其中一个字段如下所示:

from_email = models.EmailField(verbose_name="From:")

我创建了一个 ModelForm 并希望验证电子邮件地址,除非将其设置为“匿名”。我尝试了以下方法,但没有成功。

class EmailForm(ModelForm):
    class Meta:
        model = EmailHistory
        exclude = ('to_email')

    to_emails = forms.CharField()

    def clean_from_email(self):

        from_email = self.cleaned_data['from_email']
        if from_email == "Anonymous":
            return from_email
        else:
            return super(EmailForm, self).clean_from_email();

I have model called EmailHistory with one of the fields that look like this:

from_email = models.EmailField(verbose_name="From:")

I created a ModelForm and want to validate for email addresses unless it's set to "Anonymous". I tried the following, to no avail.

class EmailForm(ModelForm):
    class Meta:
        model = EmailHistory
        exclude = ('to_email')

    to_emails = forms.CharField()

    def clean_from_email(self):

        from_email = self.cleaned_data['from_email']
        if from_email == "Anonymous":
            return from_email
        else:
            return super(EmailForm, self).clean_from_email();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我入戏太深 2024-10-24 02:29:32

我怀疑问题在于,正如 文档 所解释的那样, <底层表单字段的 code>clean 方法(依次运行验证器)在表单本身的特定 clean_form_field 方法之前运行。您可能希望在表单中使用普通的 CharField,并自行将电子邮件验证添加到 clean 方法中。

I suspect the issue is that, as the documentation explains, the clean method of the underlying form field - which in turn runs the validators - is run before the specific clean_form_field method in the form itself. You'll probably want to use a normal CharField in your form, and add the email validation into the clean method yourself.

凌乱心跳 2024-10-24 02:29:32

如果不重新定义 from_email,我就无法让它工作。因此,我重新定义了它,然后使用 django.core.validators.validate_email 进行了电子邮件验证:

class EmailForm(ModelForm):
    class Meta:
        model = EmailHistory
        exclude = ('to_email')

    to_emails = forms.CharField()
    from_email = forms.CharField(required=True)

    def clean_from_email(self):
        from_email = self.cleaned_data['from_email']
        if from_email == "Anonymous":
            from_email = "[email protected]"

        validate_email(from_email)

        return from_email

I couldn't get it to work without redefining from_email. So, I redefined it and then did an email validation using django.core.validators.validate_email:

class EmailForm(ModelForm):
    class Meta:
        model = EmailHistory
        exclude = ('to_email')

    to_emails = forms.CharField()
    from_email = forms.CharField(required=True)

    def clean_from_email(self):
        from_email = self.cleaned_data['from_email']
        if from_email == "Anonymous":
            from_email = "[email protected]"

        validate_email(from_email)

        return from_email
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文