loading...

Function-Based Views Vs Class-Based Views - Django

biplov profile image Biplov ・2 min read

A view is a place where you put the logic of the application. The Views are used to perform activities like fetching objects from the database, modifying those objects if needed, rendering forms and so on.

The Django official documentation describes views as:
"A view function, or view for short, is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. "

In the earlier days of Django there was only the function based views. They were easy to implement and very useful, but while working on a large project, the views were often crowded with similar functions. This was one of the reasons for the creation of class based views.

Function Based Views

Function based views are the views in Django that are defined by functions. These python functions take a web request and returns a web response.
Function based views are most of the time based on 'What you see is what you get approach'. This is because all the logic of the view must be included.

The biggest disadvantage of this approach is the length and repetition of codes. Writing a same block of code over and over is never a good practice.

 def example_fbv(request):
    items = Item.objects.all()
    context = {
        'items' : item
    }

    return render(request, 'app/index.html', context)

This example of FBVs is easy to implement and understand, but if it was a part of a larger Django project we would see a lot of similar functions. This causes code repetition and views become long and hard to read.

Class Based Views

Class based views are Django views based on Python classes. As mentioned in the Django official documentation, 'class based views do not replace function-based views, but have certain differences and advantages when compared to function-based views'. CBVs are very easy to use in simple cases, but extending them with increasing project complexity becomes more and more difficult.

Class based views may be easy to begin with but you need to understand Python classes along with Django views to master them.

class example_cbv(View):
    model = Item
    template_name = 'app/index.html'

This class based view does the exact same thing as the function based view above. It makes code relatively shorter and reduce code duplication, but it makes code harder to understand due to implicit code flow.

Conclusion

As stated earlier class based views do not replace function-based views and there is no right or wrong. Function based views can be a better choice in some cases, whereas class based views in another. It all depends on context and need of the project.

Posted on Mar 28 by:

biplov profile

Biplov

@biplov

Python, Django and all that Jazz.

Discussion

markdown guide