
Hello Techies,
Django, one of the most popular framework, comes with in-built Login, Logout, Registration, and many more features.
In this blog, I’m covering how to use Django built-in features and develop a Django login and registration based application.
Table of Contents
Below Are The Points Of How To Develop Django Login and Registration Project
- Django User Registration Form
- Django User Login
- Django Logout View
I assume you have an idea of how to build a Django project from scratch. If No, you can refer to my previous blog where I have covered all the Django establishments. For that can refer below links.
Django Installation on Ubuntu
https://techpluslifestyle.com/technology/how-to-install-django-3-on-ubuntu/
Django Installation on Windows
https://techpluslifestyle.com/technology/how-to-install-django-3-on-windows/
How to create a Django project
https://techpluslifestyle.com/technology/steps-to-create-django-3-project/
Let’s start to develop the Django login and registration project.
Create a separate account app before coding for registration. Add all account logic related parts like registration, login, and registration to this app.
To create a new app in your project use below command
python manage.py startapp accounts
Open the settings.py file and add the accounts module in the INSTALLED_APPS variable.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'accounts' ]
Add below code to templates in settings.py file
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
Open the main url.py file and add the below code in urlpatterns list as well as import the include
from django.contrib import admin from django.urls import path, include from django.views.generic.base import TemplateView urlpatterns = [ path('admin/', admin.site.urls), path('', TemplateView.as_view(template_name='home.html'), name='home'), path('',include("accounts.urls")), ]
Let’s add base.html and home.html to template file where base.html templates help when you want to use the same code or layout in more than one place.
Base Template
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>{% block title %}Django Simple Login{% endblock %}</title> </head> <body> <header> <h1>Django Simple Login</h1> {% if user.is_authenticated %} Hi {{ user.username }}! <a href="{% url 'logout' %}">logout</a> {% else %} <a href="{% url 'login' %}">login</a> <a href={% url 'sign_up' %}">Create New Account</a> {% endif %} </header> <hr> <main> {% block content %} {% endblock %} </main> <hr> <footer> <a href="http://techpluslifestyle.com">techpluslifestyle.com</a> </footer> </body> </html>
Home Page
{% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h2>Home</h2> {% endblock %}

1. Django User Registration Form
The Django Authentication Framework provides a UserCreationForm (inherits from ModelForm class) that handles user creation. By default it provides 3 fields which are username, password 1 and password 2 (for password confirmation).
Let’s create an URL for the sign_up feature. Add the below code in accounts/urls.py file.
from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path('sign_up/', views.sign_up, name="sign_up"), ]
Create in account/view.py file write down the below code.
from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm from django.contrib.auth import login from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect from django.contrib.auth import logout from django.contrib.auth import authenticate def sign_up(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return redirect('home') else: form = UserCreationForm() return render(request, 'sign_up.html', {'form': form})
Add the below Html Code for the sign up form in accounts/templates folder.
{% extends 'base.html' %} {% block content %} <h2>Sign up</h2> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Sign up</button> </form> {% endblock %}

2. Django User Login
For login add the below URL in main accounts/urls.py file in urlpatterns variable.
from django.contrib import admin from django.urls import path from django.contrib.auth import views as auth_views from django.contrib.auth.forms import UserCreationForm from . import views urlpatterns = [ path('sign_up/', views.sign_up, name="sign_up"), path('login', auth_views.LoginView.as_view(),{'template_name': 'registration/login.html'}, name='login'), ]
By default, django.contrib.auth.auth_views.LoginView class will try to render registration / login.html template. So the basic configuration is to create a Registration folder in Accounts / Templates and add a login.html template.
{% extends 'base.html' %} {% block title %}Login{% endblock %} {% block content %} <h2>Login</h2> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> </form> {% endblock %}
Add the following code to the settings.py file which will be used for redirection purposes after successful login.
LOGIN_REDIRECT_URL='/'

3. Django Logout View
For logout add the below URL in main accounts/urls.py file in urlpatterns variable.
from django.contrib import admin
from django.urls import path
from django.contrib.auth import views as auth_views
from django.contrib.auth.forms import UserCreationForm
from . import views
urlpatterns = [
path('sign_up/', views.sign_up, name="sign_up"),
path('login', auth_views.LoginView.as_view(),{'template_name': 'registration/login.html'}, name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]
Add the following code to the settings.py file which will be used for redirection purposes after successful logout. If we do not specify the below variable then after logout page will redirect to the admin login page.
LOGOUT_REDIRECT_URL = '/'
I hope you understand every step of How to create a Django login and registration project. If you still have any query do comment below.
Refer a below GitHub link for Django login and registration project.
https://github.com/pranalikambli/django_login_registration
