Class-based vs Function-based Views in Django

In this article, we'll look at the differences between Django's class-based views (CBV) and function-based views (FBV). We'll compare and contrast and dive into the pros and cons of each approach (along with Django's built-in generic class-based views). By the end, you should have a good understanding of when to use one over the other.


One of the main uses of Django (and essentially any other web framework) is to serve HTTP responses in response to HTTP requests. Django allows us to do that using so-called views. A view is just a callable that accepts a request and returns a response.

Django initially only supported function-based views (FBVs), but they were hard to extend, didn't take advantage of object-oriented programming (OOP) principles, and weren't DRY. This is why Django developers decided to add support for class-based views (CBVs). CBVs utilize OOP principles, which allow us to use inheritance, reuse code, and generally write better and cleaner code.

We need to keep in mind that CBVs are not designed to replace FBVs. Anything you can achieve with FBVs is also achievable with CBVs. They each have their own pros and cons.

Lastly, Django offers pre-made or generic CBVs which provide solutions to common problems. They have programmer-friendly names and offer solutions to problems like displaying data, editing data, and working with date-based data. They can be used on their own or inherited in custom views.

Let's look at the different view types and learn when it's appropriate to use which.

Back to Top