Exaplaining Django Login and Logout. Django is a High-Level Web Framework and it has lots of built-in features. We can use those built-in functions for our common use of Web Application. Some of the functions are Permission and User Control, Signals, Templates, Django ORM, Access Control List, etc. Out of this Registration App, is a good example and a good thing about it is that the features can be used out-of-the-box.
With the Authentication Views, you can take advantage of the following features
- Login
- logout
- User Registration
- Change Password
- Reset Password or Forgot Password
In this guide, we are going to focus on Login and Logout Feature. For Signup, Reset password and Change password check the bellow tutorials –
- How to Create User Registration View (Sign up)
- Reset and Change Password
- How to Add Social Login to Django
Getting Started – Project Setup
Before we begin, ensure you’ve got django.contrib.auth in your INSTALLED_APPS and authentication middleware is properly configured in the MIDDLEWARE settings.
Both come already configured when you start a new Django project using the command startproject
.
In case you’re beginning a brand new project simply to follow this tutorial. Create a user using the terminal, so that you can test your login logout pages.
python3 manage.py createsuperuser
Now we need to set the template directory in the settings.py file. In your TEMPLATES add the template directory path 'DIRS': [BASE_DIR + '/templates/',],
:
TEMPLATES = [ { ... 'DIRS': [BASE_DIR + '/templates/',], ... }, ]
<!-- base.html --> <html> <head> <title>{% block title %}{% endblock title %}</title> {% block css %} {% endblock css %} </head> <body> <!-- <h1>Built-In User Registeration / Authentication App</h1> --> {% block content %} {% endblock content %} {% block javascript %} {% endblock javascript %} </body> </html>
Now we have setup our requirements. Lets get started
Configure the Django Authentication URL Routes
In your urls.py file, import django.contrib.auth.views module and add the URLconf for Login and Logout.
from django.contrib import admin from django.urls import path from django.contrib.auth import views as auth_views urlpatterns = [ path('admin/', admin.site.urls), # Login and Logout path('login/', auth_views.LoginView.as_view(), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), ]
Create a Django Login Template & Form
By default, LoginView will try to render registration/login.html
. So in your templates
folder, create a registration
folder and inside that create login.html
file.
{% extends 'base.html' %} {% block title %}Login Page{% endblock title %} {% block content %} <h2>Login</h2> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> </form> {% endblock content %}

This simple example, has the ability to validate username and password and authenticate user.
Customize the Django Login View
We can pass some parameters to LoginView, which will help us to have more control over the URL and change the behavior of the view.
Suppose you want the login.html
template somewhere else than registration/login.html
. For that, you need to pass template name in LoginView
.
path('login/', auth_views.LoginView.as_view( template_name='commons/login.html' ), name='login' ),
After a successful login, we need the redirect to the next page. We can set that next page in settings.py
by adding the below code –
LOGIN_REDIRECT_URL = 'home'
If we do not set login redirect URL, it will take to default URL /accounts/profile/
which will give TemplateDoesNotExist error.
We can also redirect a user who is already authenticated by setting redirect_authenticated_user
parameter to true. ( loginview.as_view, edirect_authenticated_user, auth_views.loginview )
path('login/', auth_views.LoginView.as_view( redirect_authenticated_user=True, template_name='commons/login.html' ), name='login' ),
Customize the Django Logout View
If you need to redirect to a template after logout, you can set the template name in LogoutView like this –
path('logout/', auth_views.LogoutView.as_view( template_name='commons/logout.html' ), name='logout' ),
And if you want to redirect to a URL after logout, you can set the next page in LogoutView like this –
path('logout/', auth_views.LogoutView.as_view( next_page='home' ), name='logout' ),
Or in settings.py file add the Logout Redirect URL.
LOGOUT_REDIRECT_URL = 'home'
Note – home
is URL name. You can keep it as you want.
# urls.py ... from django.views.generic import TemplateView urlpatterns = [ ... path('', TemplateView.as_view(template_name='home.html'), name='home'), ]
<!-- home.html --> {% extends 'base.html' %} {% block title %}Home Page{% endblock title %} {% block content %} <h2>Home</h2> {% if user.is_authenticated %} <p>You are welcome on Home Page</p> <p>Your name is - {{user.first_name}} {{user.last_name}}</p> <p>Your email is - {{user.email}}</p> <br><br> <a href="{% url 'logout' %}">Logout</a> {% else %} <a href="{% url 'login' %}">Login</a> {% endif %} {% endblock content %}
Take a look to official document – here
GitHub – Run Example Locally
Code is also available on GitHub – https://github.com/studygyaan/How-to-use-Built-In-Login-and-Logout-Authentication-System-in-Django
Clone the Repository
git clone https://github.com/studygyaan/How-to-use-Built-In-Login-and-Logout-Authentication-System-in-Django.git
Change Directory
cd How-to-use-Built-In-Login-and-Logout-Authentication-System-in-Django
Create Virtual Environment – VirtualENV
virtualenv env
Activate Virtual Environment
source env/bin/activate
Run requirements file to install libraries using Pip3
pip3 install -r requirements.txt
Run the server
python3 manage.py runserver
And open http://localhost:8000/ in your browser.