April 05, 2021
The most import takeaway from this post
CBVs and FBVs don’t replace each other
CBVs | FBVs | |
---|---|---|
Code Flow | Implicit | Explicit |
Readability | Harder | Easy |
Reusability | Great | Hard |
Specialized Functionality | No but can use generic CBVs | Handling of HTTP methods via conditional branching |
Usage of Decorators | Require extra import or method override | Straightforward |
Code Duplication | DRY | A lot |
Extendability | Easy using Mixins | Hard |
It’s hard to say which is better.
It highly depends on the context.
If you need to implement let’s say a list view, it’d be better with CBVs where you can just subclass the ListView
and override the attributes.
On the other hand if you want to make a complicated operation or handle multiple forms at the same time, FBVs will be better.
from django.http import HttpResponse
def my_view(request):
if request.method == 'GET':
# <view logic>
return HttpResponse('result')
from django.http import HttpResponse
from django.views import View
class MyView(View):
def get(self, request):
# <view logic>
return HttpResponse('result')
Reference
Django : Class Based Views vs Function Based Views
https://docs.djangoproject.com/en/2.2/topics/class-based-views/intro/