Chapter 5. User Management

Login Required – LoginRequiredMixin

Login Required – LoginRequiredMixin
Tag:

Once you build user authentication functionalities, you may want to control web page access based on users' login status. You can implement this very quickly with LoginRequiredMixin in the class-based view.

LoginRequiredMixin

LoginRequiredMixin is used to check a user's login status and control page access based on the login status. Adding LoginRequiredMixin is very simple, and you just need to inherit LoginRequiredMixin in the class-based view, in which you want to control user access based on the login status.

Two steps to implement:

  • Import LoginRequiredMixin from django.contrib.auth.mixins
  • Add LoginRequiredMixin as a parent class of the view

You must add LoginRequiredMixin before the generic view (e.g., ListView). If you put LoginRequiredMixin after the generic view, it may not work properly.

Practice

Objective:
Learn how to use LoginRequiredMixin

In this practice, we'll demonstrate how to add user access restrictions for the CRUD pages in the employee learning app.

1. Edit views.py

Edit views.py like in the code below.

employee_learning/views.py
 :
from django.contrib.auth.mixins import LoginRequiredMixin
:
class CourseList(LoginRequiredMixin, ListView):
:

class CourseDetail(LoginRequiredMixin, DetailView):
:

class CourseCreate(LoginRequiredMixin, CreateView):
:

class CourseUpdate(LoginRequiredMixin, UpdateView):
:

class CourseDelete(LoginRequiredMixin, DeleteView):
:

2. Check the results

Execute the runserver command and go to http://localhost:8000/employee-learning/course-list/. You can see that the page is redirected to the Sign In page. If you are logged in, log out first and click the link to the List page.

Login-Required--LoginRequiredMixin

IdeaNote: Login Required Decorator

The LoginRequiredMixin approach works only for the class-based view. If you want to add the same functionality for the function-based view, add @login_required before the view. The login_required module is a decorator. A decorator in Python can add new functionality to an existing object (e.g., class) without modifying the object itself.

The code below is an implementation example. To use the decorator, you need to import it first.

views.py
from django.contrib.auth.decorators import login_required

@login_required
def detailview(request, pk):
:

Once you build user authentication functionalities, you may want to control web page access based on users' login status. You can implement this very quickly with LoginRequiredMixin in the class-based view.

LoginRequiredMixin

LoginRequiredMixin is used to check a user's login status and control page access based on the login status. Adding LoginRequiredMixin is very simple, and you just need to inherit LoginRequiredMixin in the class-based view, in which you want to control user access based on the login status.

Two steps to implement:

  • Import LoginRequiredMixin from django.contrib.auth.mixins
  • Add LoginRequiredMixin as a parent class of the view

You must add LoginRequiredMixin before the generic view (e.g., ListView). If you put LoginRequiredMixin after the generic view, it may not work properly.

Practice

Objective:
Learn how to use LoginRequiredMixin

In this practice, we'll demonstrate how to add user access restrictions for the CRUD pages in the employee learning app.

1. Edit views.py

Edit views.py like in the code below.

employee_learning/views.py
 :
from django.contrib.auth.mixins import LoginRequiredMixin
:
class CourseList(LoginRequiredMixin, ListView):
:

class CourseDetail(LoginRequiredMixin, DetailView):
:

class CourseCreate(LoginRequiredMixin, CreateView):
:

class CourseUpdate(LoginRequiredMixin, UpdateView):
:

class CourseDelete(LoginRequiredMixin, DeleteView):
:

2. Check the results

Execute the runserver command and go to http://localhost:8000/employee-learning/course-list/. You can see that the page is redirected to the Sign In page. If you are logged in, log out first and click the link to the List page.

Login-Required--LoginRequiredMixin

IdeaNote: Login Required Decorator

The LoginRequiredMixin approach works only for the class-based view. If you want to add the same functionality for the function-based view, add @login_required before the view. The login_required module is a decorator. A decorator in Python can add new functionality to an existing object (e.g., class) without modifying the object itself.

The code below is an implementation example. To use the decorator, you need to import it first.

views.py
from django.contrib.auth.decorators import login_required

@login_required
def detailview(request, pk):
:
Tag: