Change Password (Default)

Prerequisite

Basically, all you need to have django.contrib.auth in your INSTALLED_APPS and an email service properly configurated (for production). In this tutorial, we are going to use console email backend which prints email in console(terminal/command line) instead of sending an email.

How to Create Change Password in Django

Implementing a built-in Change Password in Django is very easy. Django provides authentication and authorization. For Changing Password you need to get authenticated first.

In your urls.py, we need to import PasswordChangeView from Django Auth. By default, Django PasswordChangeView will render template registration/change_password.html. But we need some customization and we’ll tell PasswordChangeView to render a template from commons/change-password.html. success_url is also a way to redirect a user after changing the password successfully.

urls.py

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views

urlpatterns = [
    ...
    # Change Password
    path(
        'change-password/',
        auth_views.PasswordChangeView.as_view(
            template_name='commons/change-password.html',
            success_url = '/'
        ),
        name='change_password'
    ),
]
<!-- commons/change-password.html -->
{% extends 'base.html' %}

{% block title %}Change Password Page{% endblock title %}

{% block content %} 
    <h2>Change Password</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <a href="{% url 'home' %}">Back</a>
        <button type="submit">Submit</button>
    </form>
{% endblock content %}

Last updated