Django

What is the difference between Class Based Views and Function Based Views?

How many of you have started with a new Django project and you asked yourself what type of views it is better to use: function-based views or class-based views?
Commonly, the function-based views are the most used due to them being the first used when Django views are starting to be understood and this view type is very easy to use and functional; so then, why were class-based views created? Which view type would be the most appropriate? What’s the main difference between these two types of views? We answer these questions below.

Function-based views

First of all, we'll mention what a function is, so, as Django official documentation explains:

“A view function, or view for short, is simply 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.“

A simple example of a list function would be:

def item_list(request):
	template_name = 'app/item_list.html'
	items = Item.objects.all()
	data = {}
	data['object_list'] = items
	return render(request, template_name, data)

As you can see, this function is very easy to implement and it’s very useful but the main disadvantage is that on a large Django project, there are usually a lot of similar functions in the views; one case could be that all objects of a Django project usually have CRUD operations, so this code is repeated again and again unnecessarily, and so, this was one of the reasons that the class-based views and generic views were created!

Class-based views

As Django official documentation explains about the class-based views:

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:

  • Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
  • Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.”

As already mentioned, the class-based views don’t replace function-based views but thanks to the inheritance they are easier to implement and more optimal, furthermore, for solved much more the repeated code, the Django’s generic views were developed and with this, the class-based generic views are more optimal yet.

The example shown above would be:

class ItemList(ListView):
	model = Item

This is very easy, isn't it?

The generic views help to simplify the code much more, in that it has attributes and methods providing functionality by defect, if we wanted to modify or add attribute values or some method, we only have to write the attribute/method in our code and this will overwrite the default values.

Some generic views are: TemplateView, ListView, DetailView, CreateView, DeleteView, so on. See more here.

Conclusion

In conclusion, could it be said that it’s more appropriate to use the class-based views than the function-based views? Actually no, it’s just as good to use functions like use classes on the views, but it’ll depend on the functionality, we can use functions if the functionality is simple (show a template, for example), and we use generic views with class if the functionality contains CRUD operations or it’s more complex, as it's more optimal.

About the author

Reasons Why Your Business Should Develop a Mobile Application
03.01.2017

Reasons Why Your Business Should Develop a Mobile Application

Look around. How many technological devices do you have near you? Desktop computers, laptops, tablets and obviously ... ...

How to add Social Media buttons to Django admin
02.20.2017

How to add Social Media buttons to Django admin

The Django admin panel is one of the strong points and one of the features we like most, and one of the ways it has as a ...

Let’s have a coffee and talk about your project

START A PROJECT

Let’s have a coffee and talk about your project

START A PROJECT

We use cookies to ensure you get the best experience on our website. More info.

Accept