0
0
Djangoframework~5 mins

Password change and reset in Django

Choose your learning style9 modes available
Introduction

Changing and resetting passwords helps keep user accounts safe. It lets users update their password or get a new one if they forget it.

When a user wants to update their password for security reasons.
When a user forgets their password and needs to reset it.
When an admin wants to force users to change passwords after a security event.
When implementing user account management features in a website.
When you want to provide a secure way for users to recover access.
Syntax
Django
from django.contrib.auth.views import PasswordChangeView, PasswordResetView
from django.urls import path

# URL patterns example
urlpatterns = [
    path('password_change/', PasswordChangeView.as_view(), name='password_change'),
    path('password_reset/', PasswordResetView.as_view(), name='password_reset'),
]

Django provides built-in views to handle password change and reset.

You usually add these views to your URL configuration to enable the features.

Examples
Customize the password change page by setting your own template and success URL.
Django
from django.contrib.auth.views import PasswordChangeView

class MyPasswordChangeView(PasswordChangeView):
    template_name = 'myapp/password_change_form.html'
    success_url = '/password_change_done/'
Customize the password reset email and subject templates, and set where to go after reset request.
Django
from django.contrib.auth.views import PasswordResetView

class MyPasswordResetView(PasswordResetView):
    email_template_name = 'myapp/password_reset_email.html'
    subject_template_name = 'myapp/password_reset_subject.txt'
    success_url = '/password_reset_done/'
Sample Program

This example shows how to add all the standard Django password change and reset URLs with custom templates and success pages. It covers the full flow: change password, reset request, email, confirmation, and completion.

Django
from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('password_change/', auth_views.PasswordChangeView.as_view(
        template_name='registration/password_change_form.html',
        success_url='/password_change_done/'
    ), name='password_change'),

    path('password_change_done/', auth_views.PasswordChangeDoneView.as_view(
        template_name='registration/password_change_done.html'
    ), name='password_change_done'),

    path('password_reset/', auth_views.PasswordResetView.as_view(
        template_name='registration/password_reset_form.html',
        email_template_name='registration/password_reset_email.html',
        subject_template_name='registration/password_reset_subject.txt',
        success_url='/password_reset_done/'
    ), name='password_reset'),

    path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(
        template_name='registration/password_reset_done.html'
    ), name='password_reset_done'),

    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
        template_name='registration/password_reset_confirm.html',
        success_url='/reset/done/'
    ), name='password_reset_confirm'),

    path('reset/done/', auth_views.PasswordResetCompleteView.as_view(
        template_name='registration/password_reset_complete.html'
    ), name='password_reset_complete'),
]
OutputSuccess
Important Notes

Make sure your email settings are configured in Django to send password reset emails.

Use secure HTTPS in production to protect password data during transmission.

Customize templates to match your website style and improve user experience.

Summary

Django has built-in views to handle password change and reset securely.

You add these views to your URLs and customize templates as needed.

The full reset flow includes request, email, confirmation, and completion steps.