forms.py
from django.contrib.auth.forms import PasswordResetForm
class EmailValidationOnForgotPassword(PasswordResetForm):
def clean_email(self):
email = self.cleaned_data['email']
if not NewUser.objects.filter(email__iexact=email, is_active=True).exists():
raise forms.ValidationError("There is no user registered with the specified email address!")
return email
urls.py
from users.forms import EmailValidationOnForgotPassword
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('users.urls',namespace='users')),
path('reset_password', auth_views.PasswordResetView.as_view(form_class=EmailValidationOnForgotPassword), name='reset_password'),
path('reset_password_sent',auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
password_reset_form.html
<form method="post">{% csrf_token %}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% endif %}
<div class="form-group">
<label><strong>Email</strong></label>
{{ form.email|attr:"type:email"|attr:"class:form-control"|attr:"placeholder:Email" }}
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary btn-block">SUBMIT</button>
</div>
</form>