Django Authentication Tutorial | How to do login logout password change | Password reset
Setting up the environment
python -- version
INSTALLED_APPS = ['crispy_forms',]
INSTALLED_APPS = ['accounts', #nameOfTheApp]
If everything goes well, you can browse to navigate to the localhost. But, first, go to the web browser and write in the address bar- 127.0.0.1:8000 to see the web application up and running.
Now, we will start our built-in auth application. Django offers us most of the pre-written classes out of the box so that we do not have to reinvent the wheel. The function we will use, and Django offers us out of the box:
- Login through the LoginView class view
- Logout through the LogoutView class view
- Password reset through the PasswordResetView class view
- Password change through the PasswordChangeView class view
To implement the above functionalities, we have to provide templates in our application. Now, we will create a urls.py file inside the accounts app folder and write the following code:
from django.contrib.auth import views
from django.urls import path
urlpatterns = [
]
from django.contrib.auth import views
from django.urls import path
urlpatterns = [
path('login/', views.LoginView.as_view(), name='login'),
]
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block main %}
<div class="card">
<div class="card-body">
<h4 class="card-title">Log in to your account</h4>
<form method="post">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
{{ form|crispy }}
<button type="submit" class="btn btn-primary btn-block">Log in</button>
</form>
</div>
</div>
{% endblock %}
LOGIN_REDIRECT_URL = '/admin'
urlpatterns = [
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
]
{% extends 'base.html' %}
{% block main %}
<p>You are logged out!</p>
<a href="{% url 'login' %}">Log in again</a>
{% endblock %}
Now, we will do the password reset using the following class
- PasswordResetView
- PasswordRestDoneView
- PasswordRestConfirmView
- PasswordResetCompleteView
Now, we will set our URLs inside the urls.py files in the accounts apps URLs.
urlpatterns = [
path('password-reset/', views.PasswordResetView.as_view(), name='password_reset'),
path('password-reset/done/', views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block main %}
<div class="card">
<div class="card-body">
<h4 class="card-title">Reset your password</h4>
<form method="post">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
{{ form|crispy }}
<button type="submit" class="btn btn-primary btn-block">Reset</button>
</form>
</div>
</div>
</div>
{% endblock %}
- PasswordChangeView
- PasswordChangeDoneView
urlpatterns = [
path('password-change/', views.PasswordChangeView.as_view(), name='password_change'),
path('password-change/done/', views.PasswordChangeDoneView.as_view(), name='password_change_done'),
]
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block main %}
<div class="card">
<div class="card-body">
<h4 class="card-title"> Change your password</h4>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-success">Change password </button>
</form>
</div>
</div>
{% endblock %}
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %}Changed Password successful{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item active"><a href="{% url 'password_change' %}"></a>Change password</li>
<li class="breadcrumb-item active">Success</li>
{% endblock %}
{% block content %}
<div class="alert alert-success" role="alert">
<strong>Success!!</strong>Your password has been changed.
</div>
<a href="{% url 'home' %}" class="btn btn-secondary">Return to home page</a>
{% endblock %}
We are almost finished. Before we wrap up everything, we need to do a couple of things. Our accounts urls.py all the paths we have to connect with the project URL files. Now, go to that loginlogout/urls.py file and write
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls'))
]
Here we added include() function. That, include() function will import all the URLs from the accounts urls.py files. Another thing we need to inform the project about our all the templates where they are located. So, go to the setting file and add the following command; just add the pink color code rest of the code will there pre-written:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/'templates'],'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages',],
},
},
]
Finally, we will add the email configuration to the setting files.
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_HOST_USER='youemail@gmail.com'
EMAIL_HOST_PASSWORD='yourpassword'
Now, start the development server and write the following URLs to the web browser:
http://127.0.0.1:8000/accounts/login/
http://127.0.0.1:8000/accounts/logout/
http://127.0.0.1:8000/accounts/password-change/
http://127.0.0.1:8000/accounts/password-reset/
We have seen in this Django authentication tutorial how simply and quickly you can add to your Django project all the above functionalities.