Class Based View
Introduction
In this video, we are going to learn about class based view.
In Django, you can create view using function as well as using class.
I have already explained the how to create view using function.
Now, lets discuss about creating view using python classes.
Class Based View
Typically, to create and handle get and post request using function based view, we use to write the code in this way.
def my_view(request):
if request.method == "POST":
# POST Request Logic
pass
# Get Request Logic
return ...
Now, if you want to handle the same get and post request using class based view, then django provides View which you can import from django.views. You have to create a class and inherit this View. Then you can handle the get and post request by creating get and post method in that class.
from django.views import View
class MyView(View):
def get(self, request):
# GET Request Logic
return ...
def post(self, request):
# Post Request Logic
return ...
This is how you can create a very simple class based view in Django.
Advantages
Now, you might be wondering about why should I used class based view. Why not only function based view.
There are several advantages of using class-based views (CBVs) over function-based views in Django.
-
The first advantage is Code reusability. Class Based Views allow you to encapsulate common behavior and reuse it across multiple views. For example: You can create a base view class with common functionality, you can inherit from it in other views and avoid code duplication.
-
The second advantage is Better organization. Class Based Views promote better code organization by separating concerns into different methods and attributes of a class. This makes it easier to understand and maintain complex views.
-
The third advantage is Built-in functionality. Class Based Views provide a lot of built-in functionality that can save time and effort. For example, in the future we, will learn ListView and DetailView that provides pagination, filtering, and search capabilities out of the box. So, you don't need to write any extra code to handle these features.
-
The fourth advantage is Easier customization. Class Based Views allow you to customize behavior by overriding methods and attributes. This makes it easier to modify and extend functionality without having to rewrite large amounts of code.
-
The fifth advantage is Better readability. Class Based Views can be easier to read and understand, especially for developers who are familiar with object-oriented programming. The use of inheritance and encapsulation can make the code more intuitive and easier to reason about.
-
The sixth advantage is Separation of concerns. Class Based Views encourage separation of concerns by separating business logic from HTTP handling. This makes it easier to write testable code and to change the way data is displayed without changing the underlying logic.
Now, you have got some general understanding of why I am inclining more towards class based view and why we should learn it.
When should you use a function-based view, and when should you use a class-based view?
Based on my experience, if you need complete control of your view, then use function based view. But if you are performing any generic task like create, update, delete, list and detail operation, then you can use class based view and avoid rewriting the same code again and again.