DEV Community

Cover image for Django Send an Email For Password Reset
Joseph Mania
Joseph Mania

Posted on

Django Send an Email For Password Reset

Its obvious to forget your password. That is the reason behind the "forgotten password" link on any authentication system. When using Django, you just need less than 20 lines of code, and you are done. After building your application, then forgotten passwords should be among the last things to work on.

The process requires four templates:

Login.html: This is the login page that has a link directing the user to the second page:

Password_reset.html:
The HTML file contains an empty input to enter your valid email upon which a link will be sent.

Password_reset_done:
The templates contain information telling the user that the link has been sent to the email.

Password_reset_confirm:
This is where you are directed when the user clicks on a link sent to the email. It opens a page where you are allowed to change your password.

Password_reset_complete:
After submitting your changes, this page should open up with a new link directing you back to login.

Make sure you link up the pages in this order. You can decide to use the internal form or design your form. But Django form becomes faster and easier.

The next process is to visit the main URL containing minor URLs pages. Include the following templates in that file.
Import the auth. views first.
from Django.contrib.auth import views as auth_views

Then inside urlspatterns[] add
path('password-reset/', auth_views.PasswordResetView.as_view( template_name='users/password_reset.html'), name='password_reset'),

path('password-reset/done', auth_views.PasswordResetDoneView.as_view(

template_name='users/password_reset_done.html'), name='password_reset_done'),

path('password-reset-confirm///', auth_views.PasswordResetConfirmView.as_view(

template_name='users/password_reset_confirm.html'), name='password_reset_confirm'),

path('password-reset-complete/', auth_views.PasswordResetView.as_view(

template_name='users/password_reset_complete.html'), name='password_reset_complete'),

After setting up the following codes, make sure those names like auth_views.PasswordResetConfirmView are as indicated above. Don’t change anything even a single letter, because they are global django keywords.

In the main app settings, work on the email setup to allow Google to easily send SMTP URLs. We need the host, port number, host email, and password. If you are in the development process, ensure you allow fewer applications to access your email. If not, you might receive an error as I did.

Import os

Then at the bottom part->

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER =”your company/ur email as host”
EMAIL_HOST_USER = “Your password”

It is as simple as this. If you receive any errors kindly comment for rectification
Visit my GitHub for the full free code: https://github.com/maniamartial/djangoFullstackBlogApp

Top comments (0)