info@trickywebsolutions.com |
+1-225-276-2741
Tricky Websolutions is a part of our secret weapon. I constantly attribute part of our success to our relationship with them...
- Duncan Bell
Columbia Books & Information Services
Owing to our experience of working with Fortune 500 company products, we have been able to conceive the best of products that meet market demand
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 […]
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.
django.contrib.auth
If you haven’t already created a project, do so by running:
django-admin startproject myprojectcd myprojectpython manage.py startapp accounts
In settings.py, add the necessary apps:
settings.py
INSTALLED_APPS = [ ..., 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sites', # Required for email-based reset 'django.contrib.messages', 'accounts',]
Run the migration command to apply database changes:
python manage.py migrate
For the password reset to work, Django requires email settings to send reset links.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_HOST = 'smtp.gmail.com'EMAIL_PORT = 587EMAIL_USE_TLS = TrueEMAIL_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'
Django provides built-in views for password reset. We’ll use them directly.
In your project’s urls.py file, include the following routes:
urls.py
from django.urls import pathfrom django.contrib.auth import views as auth_viewsurlpatterns = [ # 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'),]
Django requires specific templates to render the password reset views. Create a templates/registration/ directory and add the following files.
templates/registration/
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>
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>
!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>
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>
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>
Run the Django development server:
bashCopy codepython manage.py runserver
python manage.py runserver
Visit http://127.0.0.1:8000/password_reset/ to access the password reset form. Submit your email address to test the flow.
http://127.0.0.1:8000/password_reset/
If using the console email backend, the reset link will appear in the terminal. For SMTP, check your inbox.
Click the link in the email, set a new password, and complete the process.
You can improve the default views by customizing their templates or overriding the PasswordResetView class. For example:
PasswordResetView
from django.contrib.auth.views import PasswordResetViewclass 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'),
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! 🎉
Login Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
Web Development Copy Copy
SIP Trunking Services Comparison Copy Copy
Password Reset Functionality in Django Using contrib.auth Copy Copy
Web-to-Phone System Using Asterisk Copy Copy
We will zealously try to help you by providing technical support. We are open to inquiries or requests.
info@trickywebsolutions.com
+1-2252762741
+916280560026
1945 Brightside Drive, Baton Rouge, LA -70820
We are available for a friendly chat to discuss your business needs, no obligation.