PROJECTS NOTES HOME

Class based views vs function based views in django

It is a matter of taste. BUT using classes might give you less code. A pattern to follow can be:

But when that is said, I almost always use functions because I love them. And that is also a good practice to follow

BASICALLY DOING CVB's only to get experience in OOP. OOP will be useful when working with django admin and overall, OOP knowledge is a must, I have grown to that. Even though I like function views more, let's work with CLASS BASED VIEWS.

if you are performing a more complex operation, handling multiple forms at once, a function-based view will serve you better.

If you find you’re struggling to implement your view as a subclass of a generic view, then you may find it more effective to write just the code you need, using your own class-based or functional views.

Serving a noble purpose of code de-duplication, class-based views often offer the cure that is worse than the disease. The code gets messy in an attempt to save or reuse a few lines of code.

Just ignore all generic CBVs, and stick to the View base class! You don’t need to use anything other to use CBVs and make your project work.

If you’re new to Django, or new to using class based views, it’s completely acceptable to stick to building on top of the View class. I’d go as far, as saying that it’s recommended when starting out and the best choice if you feel overwhelmed.

Simple is better than complex

The third line in the Zen of Python is a good guiding rule, and applies in this case.

Usually, it's just easier to write your own non-generic view, than trying to use them unprepared.I prefer the class-based views which are based on the View class, unless the project is large enough that there's a lot of repetition going on

CBV's: