Password Reset with PasswordResetView

A snippet for adding password reset via email using PasswordResetView.


# urls.py

from django.urls import path
from django.contrib.auth.views import PasswordResetView
from django.urls import reverse_lazy

urlpatterns = [
    path('reset-password/', PasswordResetView.as_view(
        template_name='reset_password.html',
        email_template_name='reset_password_email.html',
        success_url=reverse_lazy('password_reset_done')
    ), name='reset_password'),
]
  

# templates/reset_password.html

<h2>Reset Your Password</h2>
<form method="POST">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Send Reset Email</button>
</form>

# templates/reset_password_email.html

Click the link below to reset your password:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
  
Explanation:
  • PasswordResetView handles sending the password reset link to the user's email.
  • We provide a custom template for both the form and the email message.
  • The reset email includes a secure token-based link that takes the user to a confirmation page.
  • reverse_lazy is used for lazy resolution of the redirect URL after form submission.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.