Decorators
Introduction
In this video, we are going to learn the concept of decorators in django.
- Basically, Decorators are used to add functionality or to modify the behavior of functions or views.
- Decorators are actually a functions that wrap other functions and allow yous to perform actions before or after the wrapped function is executed.
- Decorators are commonly used in Django for tasks such as authentication, caching, permission checking, and URL routing.
- They are implemented using Python's function decorators, which are denoted by the @ symbol followed by the decorator function name.
Adding Login required decorators in Users App
-
Now, lets try to add a decorator called login required decorator in our django project.
-
So, Open your views.py file of users.
-
At first lets import the login required decorator
from django.contrib.auth.decorators import login_required
-
This login required decorator is used to check whether the user is authenticated or not. If the user is not authenticated then this decorator will redirect the user to the login page.
-
Now, we will add this decorator in all the views where we need to check whether the user is authenticated or not.
Add this decorator in
- Logout
- Change Password
- Profile View
- Profile Update
- Email Verification Request
- Email Verifier
Adding Login required and permission required in Center app
-
Till now, we have learnt login required decorator. There is one more decorator called permission required.
-
This permission required decorator is used to check whether the user have a certain permission or not.
-
Lets, see how to use that. So, open your views.py file of center.
-
Here, import the login required and permission required decorator.
from django.contrib.auth.decorators import login_required, permission_required
- Now, lets use this decorators for the center views.
@login_required
def center_list(request):
@login_required
def center_detail(request, id):
@login_required
@permission_required("center.add_center", raise_exception=True) # 403 status code
def create_center(request):
@login_required
@permission_required("center.change_center", raise_exception=True)
def update_center(request, id):
@login_required
@permission_required("center.delete_center", raise_exception=True)
def delete_center(request, id):
-
In this way, you have learnt how to use decorators in function based views.
-
Now, lets learn how to use these decorators in class based views.
Adding decorators in Vaccine views
-
Open your views.py file of vaccine.
-
At first, import the login_required and permission_required decorators.
from django.contrib.auth.decorators import login_required, permission_required
- Since, our vaccine views are class based views. So, we need a special decorator called method decorator to use login required and permission_required. Because you can't directly use any decorators in class based views which are built for function based views.
from django.utils.decorators import method_decorator
- We will use this method decorator to use any decorators built for function based views.
@method_decorator(login_required, name="dispatch")
class VaccineList(View):
@method_decorator(login_required, name="dispatch")
class VaccineDetail(View):
@method_decorator(login_required, name="dispatch")
@method_decorator(permission_required("vaccine.add_vaccine", raise_exception=True), name="dispatch")
class CreateVaccine(View):
@method_decorator(login_required, name="dispatch")
@method_decorator(permission_required("vaccine.update_vaccine", raise_exception=True), name="dispatch")
class UpdateVaccine(View):
@method_decorator(login_required, name="dispatch")
@method_decorator(permission_required("vaccine.delete_vaccine", raise_exception=True), name="dispatch")
class DeleteVaccine(View):
In this way, you have learnt how to use decorators in function based view and class based views.