Difference Between Class-based views and Function-Based Views
Django is one of a most popular framework for the web development. The reason of its popularity is that it provides the many built-in features that save lots of time of a developer. If you are beginners then it will take some time to understand the flow to the project. But once you get some experience then you can easily understand the execution of features.
Django is based on the MVT pattern means Model View Template. We mainly use the two-types of view - function-Based views and class based views. If you are beginners, you must be aware from the function based views (FBV).
When Django was introduced, it only supported the function-based view but later Django added the concept of the class-based views. The class-based views reduce the redundancy of the code. It also supports the DRY concept of the Django.
In this tutorial, we will learn in-depth how the FBV and CBC are different from each other. We can perform the same task using the function-based views and class-based views. But both approaches are different.
Requirements of Django Views
Below are the points that we should remember about the Django views.
Let's have a detailed explanation of way of creating views.
Function-based views are simple to use and beginners can easy understand them. It helps to understand the core concept of the Django fundamentals. FBV provides the advantages to understand the django concept from scratch.
Django project usually have the CRUD operations, so we need to implement same code for the multiple times unnecessarily and that's why the Django class-based views come into the scenario. The class based-views are created to solving the code redundancy problems. Let's understand the following pros and cons of the FBV.
Advantages of Function Based Views
The advantages of FBV are given below.
Disadvantages of Function Based Views
The cons of FBV are given below.
Let's understand the following implementation of FBV.
Class Based Views
Class-based views are advance way to create views in Python. It is implemented in the project as Python objects instead of functions. It is not a substitute of FBV, but they provide advantages over the function based views. It reduces the code repetition and takes cares of the basic operations such as deleting and adding item.
It is slightly hard to get the concept of class-based views for beginners. You should go through the documentation, and you will have study properly. If you have a clear idea about the function based views, you can move to the class based views.
Let's understand the following pros and cons of the views in Python.
Advantages of Class Based Views
Below are the advantages of the class based views.
Disadvantages of Class Based Views
Below are the disadvantages of the class based views.
Let's see the implementation of the class based view.
We call the as_view() method to serve the request to the user. The as_view() method calls the dispatch() method to determine which class method needs to executed, depending on the HTTP request. We can implement it in following way.
When we use the Django generic class-based views, we can over-write the helper method like get_form_class and get_template_names. We can also add some custom logic at these points instead of just overriding the class attributes.
ModelFormMixin is one of the best examples. The form_valid method is overridden with the updated value stored in self.object form_valid method is overridden.
Django Generic Class-Based View
With the help of generic class based view, we can perform some important task such as creating a new object, list views, pagination, form handling, archive views, delete view, etc.
We can implement this by importing the django.views.generic. Generic class-based views are excellent way to perform some essential tasks. It speeds up the development process.
Django provides a set of mixins, and generic class-based views. With the help of these tools we can solve the most common tasks in web development.
It saves us to writing same code again and again. In the below example, we can modify MyCreateView to inherit from django.views.generic.CreateView.
As we can observe that, it takes very less code in comparison of previous views. The django.views.generic.CreateView comes with the lots of built-in functionality and shortcuts. Let's discuss few more details.
By default template should reside in /<modelname>/<modelname>_form.html. We can modify it by setting the class attributes template_name and template_name_sufix.
This is a hot debate among the developers that which views are best to use Class-based or function-based views? We have discussed the use, cons, and pros of both types of the views. It totally depends the project requirement and what you are comfortable for. In some cases, class-based view performs well and in some case function based is better.