Creating a website is fun, but a login restrictor in your website will make it look more secure. Django REST Framework is a robust and flexible toolkit for building Web APIs. The Django login required decorator provide the feature to restrict the access
We have often visited websites in which we need to log in first before accessing or visiting other pages. In other words, restricting access.

How to restrict access with django login required decorator function ?

We also came across some of our projects where we need to do the same but wonder how? So yes you came to the right place, but before moving ahead let’s first sneak peek about the login decorator in Django Rest Framework. login_required() decorator does the following things:-

  1. Execute normally the view if the user is logged in.
  2. Redirect the user to the login_url path if the user is not logged in.


@login_required(login_url=”html page”)

In this blog, we will understand how to restrict access with the Django login required decorator function? Where to use it? And all about it.

Step 1. Create Django Project

We are first going to create a Django project, an app inside that project.

  1. Create a Django project.
django admin startproject bloglogindecorator
  1. Create app in that django-project.
python startapp logindecorator
  1. Add your app name in installed apps.


Step 2. Add files and Folder to the Django Project

We need to create a template folder in the Django folder and a file in the app folder.

  1. Create a new folder in the Django folder(here, bloglogindecorator) save it with the name template.
  2. Add the path for this template folder in bloglogindecorator>

import os
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'template')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

3. Create a new file in the app folder(here, login decorator) save it with the name

4. Add the path for this file in bloglogindecorator >

from django.contrib import admin
from django.urls import path,include
urlpatterns = [

Step 3.  Login Decorator

  1. Add database. We are first going to add our database to our project. In add the below code according to your database in DATABASES.
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'bloglogindecorator',
        'USER': 'postgres',
        'HOST': 'localhost'

2. Migrate model

Run the below code to migrate your model into your database.

python makemigrations
python migrate

3. Add login decorator to the functions which require the user to login first in logindecorator(your_app_name) > We will import login_requred from django.contrib.auth.decorators and place it before the functions where we need the user to login first following ‘@’ symbol and parameters are login_url which will specify the page to redirect if user is not login and clicking that page.


@login_required(login_url=”html page”)

from django.shortcuts import render
from django.contrib.auth.models import User,auth
from django.contrib import messages
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
    return render(request,'index.html')
def about(request):
    return render(request,'about.html')
def faq(request):
    return render(request,'faq.html')
def login(request):
    return render(request,'login.html')
def signup(request):
    return render(request,'signup.html')
def login1(request):
    if request.method=='POST':
        if user is not None:
            return render(request,'index.html')
  ,'Invalid credentials')
            return render(request,'login.html')
        return render(request,'login.html')
def signup1(request):
 if request.method=="POST":
    if pass1==pass2:
            if User.objects.filter(username=username).exists():
      ,'OOPS! Usename already taken')
                return render(request,'signup.html')            
      ,'Account created successfully!!')
                return render(request,'login.html')
  ,'Password do not match')
            return render(request,'signup.html')
def logout(request):
    return render (request,'index.html')

4. Create Url path for the function in logindecorator(your_app_name)>

from django.urls import path
from . import views
urlpatterns = [
    path('', views.index,name='index'),

5. Add function in index html page to show the links only when user is logged in otherwise show login and signup link only.


        .bodycontainer {
            background-color: rgb(214, 228, 165);
            height: 500px;
        .menucontainer {
            background-color: darkorange;
        .menucontainer a {
            text-decoration: None;
            color: #fff;
            font-size: 20px;
            padding-top: 2px;
        .menucontainer ul {
            margin-left: 900px;
            margin-top: 50px;
        .menucontainer li {
            display: inline;
            padding-left: 15px;
    <div class="container1">
        <div class="menucontainer">
                <li><a href="/">Home</a></li>
                <li><a href="about">About</a></li>
                <li><a href="faq">FAQ</a></li>
                <li><a href="login">Login</a></li>
                <li><a href="signup">Signup</a></li>
        <div class="bodycontainer">
            <h1>This is home page</h1>
            <h3>Welcome {{user.username}}</h3>

Output :-

Before Logged In :-

Before log in

After Log In :-

After log in

Quick Revision:-

  1. Create your django project folder.
  2. Create an app folder in the django project folder.
  3. Add template folder in the django folder and provide its path in django_folder > .
  4. Create file named as in the app folder and provide its path in django_project >
  5.  Add login decorator to the function in app_folder >
  6. Add restriction to pages in html code in django_project > template > index.html.

GitHub link:-