• OUR GLOBAL PRESENCE:
  • USA
  • IN
  • SG
  • PT

Django’s django.contrib.auth library simplifies implementing password reset functionality. This feature enables users to reset their passwords through email verification. In this blog, we’ll walk you through setting it up step by step.


Step 1: Setting Up Your Django Project

1. Create a Django Project and App

If you haven’t already created a project, do so by running:

django-admin startproject myproject
cd myproject
python manage.py startapp accounts

2. Add Installed Apps

In settings.py, add the necessary apps:

INSTALLED_APPS = [
...,
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sites', # Required for email-based reset
'django.contrib.messages',
'accounts',
]

3. Migrate the Database

Run the migration command to apply database changes:

python manage.py migrate

Step 2: Configuring Email Settings

For the password reset to work, Django requires email settings to send reset links.

Example Email Configuration in settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@example.com'
EMAIL_HOST_PASSWORD = 'your-email-password'

Note: Replace the email credentials with your own. For testing, you can also use Django’s console email backend:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Step 3: Setting Up Password Reset Views

Django provides built-in views for password reset. We’ll use them directly.

1. Add Password Reset URLs

In your project’s urls.py file, include the following routes:

from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
# Password Reset URLs
path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

Step 4: Creating Templates for Password Reset

Django requires specific templates to render the password reset views. Create a templates/registration/ directory and add the following files.

1. Password Reset Form (password_reset_form.html)

<!DOCTYPE html>
<html>
<head>
<title>Password Reset</title>
</head>
<body>
<h2>Reset Your Password</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Reset Password</button>
</form>
</body>
</html>

2. Password Reset Done (password_reset_done.html)

<!DOCTYPE html>
<html>
<head>
<title>Password Reset Sent</title>
</head>
<body>
<h2>Password Reset Email Sent</h2>
<p>We have emailed you instructions for resetting your password. Please check your inbox.</p>
</body>
</html>

3. Password Reset Confirm (password_reset_confirm.html)

<!DOCTYPE html>
<html>
<head>
<title>Enter New Password</title>
</head>
<body>
<h2>Set a New Password</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Change Password</button>
</form>
</body>
</html>

4. Password Reset Complete (password_reset_complete.html)

<!DOCTYPE html>
<html>
<head>
<title>Password Reset Complete</title>
</head>
<body>
<h2>Your Password Has Been Reset</h2>
<p>You can now log in with your new password.</p>
<a href="{% url 'login' %}">Go to Login</a>
</body>
</html>

Step 5: Testing the Password Reset Flow

1. Start the Development Server

Run the Django development server:

bashCopy codepython manage.py runserver

2. Access the Password Reset Page

Visit http://127.0.0.1:8000/password_reset/ to access the password reset form. Submit your email address to test the flow.

3. Check Email for Reset Link

If using the console email backend, the reset link will appear in the terminal. For SMTP, check your inbox.

4. Reset Password

Click the link in the email, set a new password, and complete the process.


Step 6: Enhancing the User Experience (Optional)

You can improve the default views by customizing their templates or overriding the PasswordResetView class. For example:

Overriding the View

from django.contrib.auth.views import PasswordResetView

class CustomPasswordResetView(PasswordResetView):
template_name = 'custom_password_reset_form.html'

Update the URL to use the custom view:

path('password_reset/', CustomPasswordResetView.as_view(), name='password_reset'),

Conclusion

With Django’s built-in password reset views, you can quickly implement a robust password recovery system in your application. Customize templates, views, and email backends to suit your needs.

Try this feature in your Django project and let us know how it works for you. Happy coding! 🎉

We will zealously try to help you by providing technical support. We are open to inquiries or requests.

+1-2252762741

+916280560026

1945 Brightside Drive, Baton Rouge, LA -70820

Contact Us

Get in touch!

We are available for a friendly chat to discuss your business needs, no obligation.

Drop a message here, and we will get back to you soon.