Django reset password with email verification [Step By Step]

Want to Learn Django Reset Password With Email Verification?

am I right? great!

I will teach step by step how to Reset Password in Django with Email Confirmation.

In this tutorial, I will teach you the whole Django forgot password step by step in an easy way.

I know the setup process is a little bit tricky but, I try my best to let you understood properly.


Django Reset Password With Email Verification

First of all, I will let you know that Django provides built-in functionality to make reset password features.

Just, by setting some stuff your Django process up for forgot the password.

Before, we start you must at least knowledge of Django basics how Django works & Django files like settings.py, urls.py, etc.

Coz, if you come here to know about this functionality that means you already made a project and app in Django.

Well, as you wish my responsibility is to teach you so, are you excited to know???

Okay! let’s dig it out.


Password Reset Django Latest Version 2.2 Step By Step

Please follow all below-given step to successfully run your reset password Django functionality without any errors.


Step 1: Go to Settings.py file of your base Django project folder

Django Reset Password With Email Verification [Step By Step]

Note: Here my base project folder name is “parcel_management” and as you can see in the above image the file called settings.py we need to open.

Step 2: Now, Just go at the end of the file in settings.py

Django Reset Password With Email Verification [Step By Step]

Place your cursor after this STATIC_URL line.


Step 3: here, we configure the email server in the Django project so just copy below-given code and paste it to the last line in settings.py file.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_HOST_USER = ‘example@gmail.com’ #Your email id
EMAIL_HOST_PASSWORD = ‘example123’  # your password
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Like This,

Django Reset Password With Email Verification [Step By Step]

Note: here you need to use your Gmail id in EMAIL_HOST_USER field and your password in the EMAIL_HOST_PASSWORD field.

EMAIL_BACKEND Field contains the name of the email backend that we used in our Django application.

EMAIL_HOST field contains a mail server which we are using in our case we are using the smtp.gmail.com server.

EMAIL_HOST_USER is just the email id from where you send emails to your users.

EMAIL_HOST_PASSWORD contains the actuall password of your email id.

EMAIL_PORT is port number that we are using to send an email.

EMAIL_USE_TLS is a boolean value either true or false for TLS Security.

That’s all, now our Django app is successfully configured with the email server. isn’t it easy?

IMPORTANT: FIRST YOU NEED TO ON LESS SECURE APP IN YOUR GOOGLE GMAIL ACCOUNT FOR THAT JUST FOLLOW THIS INSTRUCTIONS

1. Search on google “Less Secure app“. [first Login with your Gmail account which you are using in this django]

Search on google "Less Secure app".

2. Now, ON Allow less secure apps [Must logged in with your gmail id that you are going to use]


Step 4: Go to the urls.py file of your base project directory.

Django Reset Password With Email Verification [Step By Step]

Note: This urls.py file is the main base project file, not an app file.


Step 5: Add this Built-in Django Packages for Reset Password in the urls.py file.

from django.contrib.auth.views import PasswordResetView, PasswordResetDoneView, PasswordResetConfirmView, PasswordResetCompleteView
Django Reset Password With Email Verification [Step By Step]

Step 6: Now, just add these paths to the same file called ‘urls.py’.

path('reset-password', PasswordResetView.as_view(), name='password_reset'),
path('reset-password/done', PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset-password/confirm/<uidb64>[0-9A-Za-z]+)-<token>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset-password/complete/',
PasswordResetCompleteView.as_view(),name='password_reset_complete'),

Like this,

Django Reset Password With Email Verification [Step By Step]

Note: Please copy the above-given code exact to get no errors.

Here, we are adding 4 paths for running password reset features.

  1. password_reset
  2. password_reset_done
  3. password_reset_confirm
  4. password_reset_complete

These views default given by just adding packages from django.contrib.auth.views class.


Step 7: here is the complete code of urls.py file just check it with your url.py file for cross-checking

IMPORTANT: THIS CODE IS JUST FOR CROSS CHECK PURPOSE DON’T ADD IT AGAIN IF YOU ADDED ABOVE STEPS CODE. JUST CHECK THIS CODE AND VERIFY WITH YOURS.

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.views import PasswordResetView, PasswordResetDoneView, PasswordResetConfirmView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('reset-password', PasswordResetView.as_view(), name='password_reset'),
    path('reset-password/done', PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('reset-password/confirm/<uidb64>[0-9A-Za-z]+)-<token>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('reset-password/complete/', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

Step 8: That’s all, Now just enter URL for reset-password in the browser after starting your Django development server.

The URL should be like this,

http://127.0.0.1:8000/reset-password

Now, it’s time to show you output.


OUTPUT

1) reset-password Page

 password-reset Page Django

2) reset-password/done Page [ Email successfully sent to user]

2) reset-password/done Page [ Email successfully sent to user]

3) Reset Password Verification Email Received from Django app

3)  Reset Password Verification Email Received from Django app

4) reset-password/confirm/ page to Set New Password

4) reset-password/confirm/ page to Set New Password

5) reset-password/complete/ Page that shows the password successfully reset.

5) reset-password/complete/ Page that shows the password successfully reset.

Now, your functionality properly works!


Related FAQ’S for Reset Password in Django

1) reverse for ‘password_reset_complete’ not found. ‘password_reset_complete’ is not a valid view function or pattern name Django

–> For this error, first of all, you need to read the above step properly.

basically, this error occurs because instead of password_reset_complete use PasswordResetCompleteView in the import section. [ I already gave above]

2) import error: cannot import name ‘passwordresetcomplete’

–> You need to use PasswordResetCompleteView instead of passwordresetcomplete.


Final Word

So, this is how we can successfully make Django reset password functionality in our web application.

As we all know Django provides lots of in-built facilities to easily develop a web application.

In very less time we successfully configured an email server to your Django app for the reset password email verification.

I hope you like this step by step tutorial let me know in a comment below if you face any problem.

Happy Coding?

1 thought on “Django reset password with email verification [Step By Step]”

Leave a comment