Login Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
info@trickywebsolutions.com |
+1-225-276-2741
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.
If you haven’t already created a project, do so by running:
django-admin startproject myproject
cd myproject
python manage.py startapp accounts
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',
]
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.
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'
Django provides built-in views for password reset. We’ll use them directly.
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'),
]
Django requires specific templates to render the password reset views. Create a templates/registration/
directory and add the following files.
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>
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
Visit http://127.0.0.1:8000/password_reset/
to access the password reset form. Submit your email address to test the flow.
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:
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'),
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
December 10, 2024 | 0 Comments
December 10, 2024 | 0 Comments
SIP Trunking Services Comparison Copy Copy
December 10, 2024 | 0 Comments
Password Reset Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
Web-to-Phone System Using Asterisk Copy Copy
December 10, 2024 | 0 Comments
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.