We have used:
- Python 3.6+
- django 3.0+
Assumption of audience
It is assumed that you(the audience) have a moderate understanding of django and python and you can create models and view.
Description
When programming in django we come across a topic of Class Based View(CBV) and Function Based View(FBV). In this article we will discuss about how to use them and which one to use in what scenerio. The point of this article is to give Django developer a scenerio when to use FBV over CBV and vice versa.
Table of content
Models Creation
To explain this concept lets create a model in django. In this article we will create a simple weather data storage system.
Lets add some data in the models List of images added
Function Based View(FBV)
In the begining of django development there was only function based view, later developers started noticing some feature were redundent and tried to extend view using various generic view. However, it was consistently hard to extend function based view. Therefore, later they introduced class based views. This example has 2 function based views 2 for listing data and another for displaying individual item.
FBV List View
We can list item in function based view as shown below. Since this is a list view it is always good practice to paginate data and present them in small chunks with links to move to new data.
Here is our views.py with function FBV_list_veiw
This view can be rendered like this in urls.py
The templates looks like this in list.html
FBV Detail View
For detail view we just pull the data based on id or slug. Here in this tutorial we are going to use it using id. Detail view function looks like this.
We will add path like this in urls.py
Class Based View(CBV)
For class based view we will use generic classes and render the same thing. Class based view makes it much easier to write our code in less amount of time.
CBV List View
Following is the code for doing the same thing as list view did in function based view. In veiws.py we will make our view class.
We can link this in url using following code snippet
The main point to notice here is .as_view() method call. In function based view we call functions directly but in class based view we call as_view() method. We can also say urls simply have methods call in them.
CBV Detail View
Detail view has following code in it
urls looks like this
Comparision of CBV and FBV
In the begining of the django there was only function based views later they introduced the concept of class based view to simply some repetative task. Lets compare advantages and disadvantages in CBV and FBV
Advantages of CBV:
- The code becomes concise and saves time.
- We can use already built in generic views, this makes it easy to extend the class
- We use separate get and post method to handle various request.
Disadvantages of CBV:
- It hides a lot of implementation detail which make it harder to read and maintain.
- It difficult for beginners to wrap head around in the first place. Once they understand the flow it becomes easier.
- For using decorators we need to import separte method called method_decorator(syntax @method_decorator(…))
Advantages of FBV:
- The code is more explicit and make it easy to read and understand.
- Easy to use decorators (like login_required and so on)
- Easy to understand for beginners
Disadvantages of FBV:
- It uses if else statement for various request like GET and POST
- It difficult to extend commonly used functionality for instance pagination.
Conclusion
When should we use which.
In my opinion both FBV and CBV completes the job. I would recommend using CBV in most of the cases as it is simple to extend and add functionality. However, if we need to write code that needs explicit explanation(like implementing an complex algorithm) FBV might be right approach. Also task like serving pdf files, or other resources can take FBV.
I hope you like this article please feel free to contact me if you have any question.