How to Reset password in Django

Django Reset Password

The Django framework comes with support for resetting user passwords. This is implemented for the Admin app, but it is possible to re-use from your own screens.

 

Requirements

  1. password_reset_form: Form where the user submits the email address.
  2. password_reset_email: Send a message containing the reset password address to the user’s mailbox. Email addresses need to be generated dynamically to prevent unwanted users from disturbing them.
  3. password_reset_subject.txt: Subject of the mail.
  4. password_reset_done: Show a successful message to the website user. Usually with instructions to open the email account, look in the spam folder, etc. And asking for the user to click on the link he will receive.
  5. password_reset_confirm: After clicking on the link in the mailbox, the user goes to the page where the password is reset.
  6. password_reset_complete: Show the user a message of a successful reset.

password_reset_form.html

{% extends 'index.html' %}

{% block content %}
  <h3>Forgot password</h3>
  <br>
  <p> Please specify your email address to receive instructions for resetting your pasword </p> 
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
  </form>
{% endblock %}

password_reset_email.html

{% 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 ‘password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
 
Your username, in case you've forgotten: {{ user.username }}
 
Thanks for using our site!
 
The {{ site_name }} team.
 
{% endautoescape %}

password_reset_subject.txt:

{{ site_name}} password reset

password_reset_done.html

{% extends 'index.html' %}
{% block content %}
    <h3>Password reset </h3>
    <br>
    <p>We've e-mailed you instructions for setting your password to the e-mail address you submitted.You should be receiving it shortly.</p>
    <br><br>
{% endblock %}

password_reset_confirm.html

{% extends 'index.html' %}

{% block content %}
  {% if validlink %}
    <h3>Change password</h3>
    <form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Change password</button>
    </form>
  {% else %}
    <p>
      The password reset link was invalid, possibly because it has already been used.
      Please request a new password reset.
    </p>
  {% endif %}
{% endblock %}

password_reset_complete.html

{% extends 'index.html' %}
 
{% block content %}
<h3>Success</h3>
<p>Your password has been set.  You may go ahead and log in now.</p>
<p><a href="{% url 'login' %}">Log in</a></p>
{% endblock %}

Configuration to send email

Django project has settings.py variables to be set for enabling  SMTP server.  Basically all you need is to have django.contrib.auth in your INSTALLED_APPS and an email service properly configured

settings.py

INSTALLED_APPS =[
       'django.contrib.auth',
]
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example@gmail.com'
EMAIL_HOST_PASSWORD = 'example123'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

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 actual password of your email id.

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

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

 

Config Url’s

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

urls.py

from userinfo.forms import PasswordResetForm

The HTML form you have where the user submits the email address.

Now, just add these URLs to the same file called ‘urls.py’.

urlpatterns = [
   url(r'^password_reset/$', auth_views.password_reset,
   name='password_reset'),
   url(r'^password_reset/done/$', auth_views.password_reset_done,
   name='password_reset_done'),
   
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.password_reset_confirm, name='password_reset_confirm'),
url(r'^reset/done/$', auth_views.password_reset_complete, name='password_reset_complete'),

Or you can simply include all auth views:

urlpatterns = [
    url('^', include('django.contrib.auth.urls')),
]

 

How to Reset password in Django
You may Also Like
Scroll to top