When they came out, i was excited. I really enjoyed the added functionality that models and forms had implemented using class like interfaces. Turns out, class based views weren't as loved by the lot. By the time I realised this I had already jumped into learning and using them. Several blogposts have been made, reviews written, tears shed, pain shared, guilt exposed and then, the dust has covered them. People have come to terms with their views, functional, or class-based.
I for one choose to use them. Because I like them. I like writing classes. A quality that has stuck, since my C++ days in college. At first, I decided to write this article in adamant defense of what I have begun to depend on more and more (I know almost the entire codebase of class-based views), but then I realised that I couldn't honestly do that, simply because, I had to know almost the entire codebase of class-based views to use them to their fullest. Therefore, my aim in this post is to provide a simple reference for usage of class-based views for simple tasks without knowing the code base or glancing at the docs.
Instead I will write this about them. They can be saviours or villains, one should use them where they save lives, but run from them where they become a burden (though, they will never kill), this is very doable considering we all have functional views to fall back on.
Let us say, I need to do a simple task. I need to create a view, that displays some data from a model. Lets try to do it with class-based views. I google, class based generic-views, something called ListView catches my eye. It looks cool, it allows me to display data from my model very easily, it also allows me to paginate it simply by passing parameters into a url.
from django.views.generic import ListView