【问题标题】:Password Reset Custom Validation Message密码重置自定义验证消息
【发布时间】:2019-07-04 17:08:44
【问题描述】:

我正在使用 django 的内置重置密码。现在的问题是,当我输入数据库中不存在的电子邮件时,它不会给出电子邮件不存在的错误。我如何在 Django 重置密码中进行验证。如果 auth_table 中不存在电子邮件

 # Reset Password
path('password-reset/',
     auth_views.PasswordResetView.as_view(
         template_name='commons/password_reset/password_reset.html'
     ),
     name='password_reset'),
path('password-reset/done/',
     auth_views.PasswordResetDoneView.as_view(
         template_name='commons/password_reset/password_reset_done.html'
     ),
     name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
     auth_views.PasswordResetConfirmView.as_view(
         template_name='commons/password_reset/password_reset_confirm.html'
     ),
     name='password_reset_confirm'),
path('password-reset-complete/',
     auth_views.PasswordResetCompleteView.as_view(
         template_name='commons/password_reset/password_reset_complete.html'
     ),
     name='password_reset_complete'),

【问题讨论】:

  • 基本上 django reset-password 不会给出任何电子邮件不存在的错误消息
  • 你需要自定义django core reset-password添加邮箱不存在错误
  • 我可以自定义错误
  • 查看以下答案
  • 从安全的角度来看,您不应该告诉用户该电子邮件不存在。

标签: django django-forms django-templates django-views


【解决方案1】:

在表单中,返回 email_id。

def clean_email(self):
    email_id = self.cleaned_data['email']
    if not User.objects.filter(email__iexact=email_id, is_active=True).exists():
        raise ValidationError("Email Invalido! Usuario Inactivo o Inexistente")
    return email_id

这对我来说效果很好。

【讨论】:

    【解决方案2】:

    你可以这样做

    在forms.py中

    from django.contrib.auth.forms import PasswordResetForm
    
    class CustomEmailValidationOnForgotPassword(PasswordResetForm):
        def clean_email(self):
            email_id = self.cleaned_data['email']
            if not User.objects.filter(email__iexact=email_id, is_active=True).exists():
                raise ValidationError("Email invalid!")
    
            return email
    

    在 urls.py 中

    url(r'^password-reset/$',
        'django.contrib.auth.views.password_reset',
        {'post_reset_redirect': '/user/password/reset/done/',
         'html_email_template_name': 'registration/password_reset_email.html',
         'password_reset_form': CustomEmailValidationOnForgotPassword},
        name="password_reset"),
    

    希望对你有帮助

    更多详情请关注link

    【讨论】:

    • 我试图了解 Django 是如何工作的。 clean_email 方法在哪里调用?如果我正在自定义 PasswordResetForm 之类的表单,我怎么知道如何调用我添加的新方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2018-12-17
    • 1970-01-01
    • 2016-02-29
    • 2013-06-05
    • 2018-01-29
    • 2012-12-12
    相关资源
    最近更新 更多