In this tutorial we’ll add a password reset sequence to our Django application. This builds upon our previous work where we added Login & Logout pages and then a Signup page.

Complete source code can be found on Github if you get stuck along the way.

Django auth app

What we want is a password_reset page where the user can enter their email address, and be sent a cryptographically secure email with a one-time link to a reset page. Fortunately Django has us covered.

If you recall the complete set of views and URLs provided by the Django auth app, there are already several for resetting a password.

accounts/login/ [name='login']
accounts/logout/ [name='logout']
accounts/password_change/ [name='password_change']
accounts/password_change/done/ [name='password_change_done']
accounts/password_reset/ [name='password_reset']
accounts/password_reset/done/ [name='password_reset_done']
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/reset/done/ [name='password_reset_complete']

The default templates however are quite ugly and we need to customize them. For example, here are the default password reset and password reset done pages:

Django password reset

Django password reset done

But first we need to setup a way to deliver, or at least fake deliver, our email messages.

SMTP Server

In the real-world you would integrate with an email service like MailGun or SendGrid. For development purposes Django lets us store emails either in the console or as a file. We’ll choose the latter and store all sent emails in a folder called sent_emails in our project directory.

To configure this, update our settings.py file by adding the following two lines at the bottom under our redirect URLs.

# my_project/settings.py
EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"
EMAIL_FILE_PATH = os.path.join(BASE_DIR, "sent_emails")

Now let’s change the appearance of the password reset pages.

Password Reset Form

The default template for password reset is located at templates/registration/password_reset_form.html. We can customize it by creating our own password_reset_form.html file:

(accounts) $ touch templates/registration/password_reset_form.html

Then add the following code:

<!-- templates/registration/password_reset_form.html -->
{% extends 'base.html' %}

{% block title %}Forgot Your Password?{% endblock %}

{% block content %}
  <h1>Forgot your password?</h1>
  <p>Enter your email address below, and we'll email instructions for setting a new one.</p>

  <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Send me instructions!">
  </form>
{% endblock %}

If you refresh the page at http://127.0.0.1:8000/accounts/password_reset/ you can see our new update:

Django password reset page new

Now go ahead and enter the email address that matches an actual user you’ve created. Then click on the button to submit it.

Upon successful submission, we’re redirected to the Password reset done page which is also ugly. Let’s change it. The default template is located at templates/registration/password_reset_done.html. So as before, create a new template file at this location and then add the following code:

(accounts) $ touch templates/registration/password_reset_done.html
<!-- templates/registration/password_reset_done.html -->
{% extends 'base.html' %}

{% block title %}Email Sent{% endblock %}

{% block content %}
  <h1>Check your inbox.</h1>
  <p>We've emailed you instructions for setting your password. You should receive the email shortly!</p>
{% endblock %}

If you refresh the password reset done page at http://127.0.0.1:8000/accounts/password_reset/done/ we can see our new page.

Password reset done page new

Password Reset Confirm

Remember how we configured our Django project to store emails in a local folder called sent_emails? If you look at your project now that folder exists! The format for the txt file will look something like this:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Password reset on 127.0.0.1:8000
From: webmaster@localhost
To: will@wsvincent.com
Date: Mon, 22 Oct 2018 16:48:08 -0000
Message-ID: <154022688842.8329.13335839467917011243@1.0.0.127.in-addr.arpa>


You're receiving this email because you requested a password reset for your user account at 127.0.0.1:8000.

Please go to the following page and choose a new password:

http://127.0.0.1:8000/accounts/reset/MQ/50n-794b1459497cbe0ed399/

Your username, in case you've forgotten: wsv

Thanks for using our site!

The 127.0.0.1:8000 team

This contains Django’s default language which we can customize. But the important section for now is the URL included. In the email above, mine is http://127.0.0.1:8000/accounts/reset/MQ/50n-794b1459497cbe0ed399/. Copy and paste this into your browser and you’ll be automatically routed to the Password reset confirmation page.

Django password confirm page

Ugly, no? Let’s create a new template with our familiar steps. The template is located by default in templates/registration/password_reset_confirm.html, so let’s create it.

(accounts) $ touch templates/registration/password_reset_confirm.html

And enter new code:

<!-- templates/registration/password_reset_confirm.html -->
{% extends 'base.html' %}

{% block title %}Enter new password{% endblock %}

{% block content %}

{% if validlink %}

<h1>Set a new password!</h1>
<form method="POST">
  {% csrf_token %}
  {{ form.as_p }}
  <input type="submit" value="Change my password">
</form>

{% else %}

<p>{% trans "The password reset link was invalid, possibly because it has already been used.  Please request a new password reset." %}</p>

{% endif %}
{% endblock %}

Refresh the page at http://127.0.0.1:8000/accounts/reset/Mg/set-password/ and you’ll see our new template.

Password confirm page

Password Reset Done

Go ahead and create a new password in our form. Upon submission you’ll be redirected to our final default page which is for Password reset complete:

Django password reset done page

To customize this page we’ll create a new password_reset_complete.html template and enter our new code:

(accounts) $ touch templates/registration/password_reset_complete.html
<!-- templates/registration/password_reset_complete.html -->
{% extends 'base.html' %}

{% block title %}Password reset complete{% endblock %}

{% block content %}
<h1>Password reset complete</h1>
<p>Your new password has been set. You can log in now on the <a href="{% url 'login' %}">log in page</a>.</p>
{% endblock %}

Now reset the page at http://127.0.0.1:8000/accounts/reset/done/ and view our work.

Password reset done page

Add to home page

Let’s add the password reset link to the homepage now so that logged-in users will see it. We can use the built-in tag {% url 'password_reset' %}. Here’s the code.

<!-- templates/home.html -->
{% extends 'base.html' %}

{% block title %}Home{% endblock %}

{% block content %}
{% if user.is_authenticated %}
  Hi {{ user.username }}!
  <p><a href="{% url 'logout' %}">logout</a></p>
  <p><a href="{% url 'password_reset' %}">reset password</a></p>
{% else %}
  <p>You are not logged in</p>
  <a href="{% url 'login' %}">login</a>
{% endif %}
{% endblock %}

Conclusion

We’ve now implemented a robust user authentication flow for our web app with login, logout, signup, and password reset. Congrats!

What else might we want? Using a custom user model instead of the built-in User model is a good idea. The Django docs note one is highly recommended. I have a separate tutorial on how to extend a custom Django user model that is worth checking out.

You could also add a “Password Change” feature. You can see an implementation of that in my open source DjangoX starter project.

Or link up a third-party provider to send real emails which I cover in my book Django for Beginners. Or try using Docker and adding full security as I cover in Django for Professionals.