P
P
Pavel Dyukarev2018-03-16 12:38:18
Django
Pavel Dyukarev, 2018-03-16 12:38:18

What do you choose and why? FBV (function views) vs CBV (class views) in Django?

Back in those distant times (2 years ago), when I was just starting to learn Python, one postulate from the philosophy of this wonderful language sunk into my head: Explicit is better than implicit.
And after a while I started learning Django.
Learned how the view function works:

def view(request):
    queryset = Model.objects.all()
    context = {'qs':queryset}
    return render(request, 'template.html', context)

Everything is simple and clear and most importantly clearly.
Receive request(arguments) --> CRUD(create, read, update, delete) --> send back
But then I stumbled upon class-based views.
And here, to be honest, I am constantly tormented by the question - why?
If I implement with CBV what I implemented with a regular function, this is what I get:
class View(ListView):
     queryset = Model.objects.all()
     template_name = "template.html"

Indeed, twice as long.
But is it worth it?
After all, firstly, in the urls I have to attribute .as_view() to the class.
Secondly, the name of the context of my queryset query variable is not visible and I have to constantly keep in my head that the name will have to be looked at somehow.
Write a loop in the template that will retrieve my variable:
{% for obj in object_list &}
 {{ obj.object_from_db}}
{% endfor %}

Moreover, if we used FBV, then we could simply write the
name of the context variable in the template (which we ourselves set in the dictionary).
{{ qs }}
So the code became longer.
Thirdly, it's just, as it seems to me, an unnecessary abstraction, albeit a beautiful one. After all, classes are needed in order to create many objects that are essentially the same but with different states. And the idea is, as it were, always the same and is not going to "breed".
And in general, it seems that from the point of view of OOP, CBV is an evolutionary logical approach. But from the point of view of humanity / explicitness, the approach is not very good.
And the length of the 'data retrieval' code in the template balances the length of the code in the views.
It's my opinion. The opinion of a beginner who cannot answer the question why and feels infringed, because as they say in the CBV documentation, this is even cooler))
Please share your opinion about the advantages of CBV.
Maybe I'm not looking at the right angle or I don't understand something.
Where do you use it, when and why?
I would appreciate any explanation!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2018-03-16
@PavelDuk

I choose CBV. They are easier to read, each part is separated. Separately getting the queryset, separately initializing the form, and so on. In FBV, everything is lumped together and in any more or less non-trivial case, the function quickly grows in size and becomes less and less clear. Especially when you start using huge operators ifto separate the parts that handle POSTand GET. Over time, FBVs grow and become less and less readable.
A big advantage of CBVs is also that they can be inherited from each other. When it becomes necessary to make several Views with similar functionality, this approach allows you to radically reduce the amount of code and increase its understandability.

T
tema_sun, 2018-03-16
@tema_sun

I would suggest sticking to the rule that if you don't know which option to choose, then use CBV. The question is not the length of the code, but the understanding of it by other developers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question