Inform user that email is invalid using Django's Password Reset

前端 未结 2 2067
無奈伤痛
無奈伤痛 2020-12-14 22:59

I am using the built-in django password reset functionality. The documentation states:

If the email address provided does not exist in the system, thi

2条回答
  •  春和景丽
    2020-12-14 23:01

    So I finally figured it out myself. Here's my implementation:

    class EmailValidationOnForgotPassword(PasswordResetForm):
        def clean_email(self):
            email = self.cleaned_data['email']
            if not User.objects.filter(email__iexact=email, is_active=True).exists():
                raise ValidationError("There is no user registered with the specified email address!")
    
            return email
    

    You also need to add {'password_reset_form': EmailValidationOnForgotPassword} to urls.py. Here's an example:

    url(r'^user/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': EmailValidationOnForgotPassword},
        name="password_reset"),
    

提交回复
热议问题