PART-15

Making a Reset Password Request With Django

Atufa Shireen
4 min readNov 25, 2020

It’s now the time to add the reset password functionality. Without throwing all the URLs or imports or settings required, let me take you to step by step. We need a URL for a reset password, right?, So jump in the blog_project/urls.py and add the URL or link for the password reset, using the built-in PasswordResetView present in the auth view of django.contrib and specify the url and template name there.

...
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),
name='password_reset'),

As we specified the template name there, let’s go create that password_reset file (where the other files login, logout exists) in the users/templates/users folder

<h1>Trouble Signing In?<br>
<span>request a password reset, and we will have you up again..</span>
</h1>
<form method='post'>
{%csrf_token%}
{{form}}
<input type="submit" value="forgotten password" class="submit-btn">
</form>

<small class="text-muted">
try signing in again? <a class="ml-2" href="{% url 'login' %}">Log Into Your Account</a>
</small>

Run the server, and go to the /password-reset/. Hopefully, you will see the form but when you send the data., Did you see this?

There’s two things over there, first, it is trying to find a reverse for password-reset that is password_reset_confirm URL, and second that it did not found the two params, uidb64 and token. So let’s create the password reset confirm URL in the urls.py module.

...
path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='users/pas sword_reset_confirm.html'),
name='password_reset_confirm'),

Create the password_reset_confirm template which takes a form similar to the reset template, and run your server. You might get another error which says…Connection Refused..?

Well, over here it is because we are trying to send an email without a connection. I will be using a Gmail account for this, whereas setting up for any other mail address could be different.

Hop in your settings.py file and add the following settings as required,

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_NAME')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')

Now, run the server, just to get this?…

Again you need a password_reset_done URL, so add this URL, its template, and URL name.

...
path('password-reset-done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),
name='password_reset_done'),

The template could just contain the text

<p>
An email has been sent you, please check your inbox and follow the instructions or the trash in case you didn’t find one.</p>

Run your server, {spoiler: You will get another error as we’re going step by step or error by error}

fill in your data again, you’ll be redirected to the password-rest-done, check your email, hopefully, to get that reset link, something like this…

follow it, you’ll be redirected to the confirm page, fill in the form, and hit that reset button

you’ll get this….

We are almost done, it’s just asking for a password-reset-complete URL. Let us add this up...

...
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'),
name='password_reset_complete'),

Create the template for it,

<h1>Your password has been reset, you can <a href="{%url 'login'%}>login to your account now</a> </h1>

And voila you have successfully reset your password, try it again and make sure you have logged out before trying to make a password reset request…

You can add the link to the reset password on the login page, or actually add functionality that the user cannot access this page unless he is not registered from any other page.

--

--