Skip to content

Reset Password - Part 1

  • In this video, we are going to implement the feature of reset password in django.
  • I would also like to mention that, reset password feature is already implemented in django.
  • In this video, we are going to re-use the existing functionality which django has provided for reset password. Using this approach, you will learn how to use existing functionalities of django and use it in your own context.
  • This video might be little bit confusing, because I will be inherinting lots of things from django and using it in my own context. But believe me, as a django developer, you should know how to use existing functionality and modify its context to meet your business requirements.

Resources

Password Reset View

Open your views.py file of user and import DefaultAuthViews

Views

from django.contrib.auth import views as DefaultAuthViews

This Default Auth Views provides us 4 different types of views which are required for password reset. In this video, we will use two of its views and reset of 2 views, we will learn in next video.

Basically, in this video and in next video, we will inherit the views related to password reset and provide our own templates and form.

At first, lets start with password reset view.

# Create a view named PasswordResetView and inherit DefaultAuthViews.PasswordResetView
# This view is responsible for showing the form to enter email address and sending password reset link to that email address.
class PasswordResetView(DefaultAuthViews.PasswordResetView):

    # In this view, at first specify the email template name. 
    email_template_name = "user/password-reset-email.html"
    # [Jump to email template section and start explaning]

    # Then specify form_class and set it to Password Reset Form.
    form_class = PasswordResetForm
    # [Jump to form section and start explaning.]

    # Now specify the success url to show the user a page that tells sending password reset link is successful. 
    # Import reverse_lazy: from django.urls import reverse_lazy
    success_url = reverse_lazy("accounts:password-reset-done")

    # Then, specify the template name of this view.
    template_name = "user/password-reset.html"
    # [Jump to the template section and start explaning.]

[Run the development server and see the changes.]

Forms

Open your forms.py file and import the PasswordResetForm

from django.contrib.auth.forms import PasswordResetForm

Basically, we will add form control class to every field of this form.

class PasswordResetForm(PasswordResetForm):
    def __init__(self, *args, **kwargs):
        super(PasswordResetForm, self).__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs["class"] = "form-control"

After that, import this form in views.py

from user.forms import PasswordResetForm

Urls

Now, create a url path for the view which we created.

path("password-reset/", views.PasswordResetView.as_view(), name="password-reset"),

In the login.html, we have to update the url for reset password.

<a href="{% url 'accounts:password-reset' %}">Reset Password</a>

Templates

  • Create a html file named "password-reset-email.html" in the templates of user.

  • This html file will contain the format of email template which user will get once the user sends password reset request.

  • I have attached password-reset-email.html file in the resources section. Download it, copy its content and paste it in the html file.

{% autoescape off %}
You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.

Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}URL
{% endblock %}

Thanks for using our site!

The {{ site_name }} team.

{% endautoescape %}

Create a template named "password-reset.html". In this html file, we will render our form.

I have attached a template named "password-reset.html" in the resources section. Download it, copy its content and paste it here in this file.

{% extends 'mysite/base.html' %}

{% block title %} 
    <title>Reset Password</title>
{% endblock title %}

{% block content %}
<div class="m-2 p-2">
  <h4>Enter the email address to reset your password</h4>
  {% include "components/form.html" with form_name="Reset Password" %}
</div>
{% endblock content %}

Password Reset Done View

Till now, we have created a view that sends password reset email to the user. Now, once the user submits the email address, we need to show a page that says something like we have sent password reset email link. Check your inbox.

View

To create such type of view, its very simple. Django has provided password reset done view. Just inheirt it and modify its template to use it in your own context.

class PasswordResetDoneView(DefaultAuthViews.PasswordResetDoneView):
    template_name = "user/password-reset-done.html"

Templates

Now, create a template named "password-reset-done.html". I have attached "password-reset-done.html" file. Download it, copy its content and paste it in the html file.

{% extends 'mysite/base.html' %}

{% block title %} 
    <title>Email Sent</title>
{% endblock title %}

{% block content %}
<div class="m-2 p-2">
    <p>We've e-mailed you instructions for setting your password to the e-mail address you submitted.</p>
    <p>You should be receiving it shortly.</p>
</div>
{% endblock content %}

URL

Now, create a url path for this view.

path("password-reset-done/", views.PasswordResetDoneView.as_view(), name="password-reset-done"),

[Run the development server and see the changes.]