Should a new Django project use class or function based views?

In a new Django project, I'm just wondering if you should use cool views (CBV) or functional views (FBV).

According to the Django documentation :

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace functional representations, but they have certain differences and advantages in comparison with function-based representations.

Which seems to contradict python Zen 'There is only one way to do this'

So which is better?

So far I see only three possibilities:

  • Always use FBV

    This means that you should not use general representations at all (since they are based on classes with 1.5)

  • Always using CBV:

    Who has certain problems with defining request processing orders. Cm

    http://lukeplant.me.uk/blog/posts/djangos-cbvs-were-a-mistake/

    I also think that creating an entire class hierarchy is not very good for performance. In this case, I also asked myself, why are FBVs not yet out of date?

  • Input of total CBV to FBV, according to

    https://gist.github.com/spookylukey/2596285

    leading to a lot of brutal boilerplate code

Do you see any other ways, or does anyone know where the shows go?

+8
python django
source share
1 answer

This is a matter of opinion, I personally disagree with Luke Plant on this, and I fell in love with Class Based Views . I think that most of the resistance of the Django community eagerly accepted them because they couldn’t easily see how they worked (the implementation uses a lot of mixins and it can be difficult to follow), and the documentation was not enough, which I think that there were many misunderstandings regarding Generic CBV and simple CBV. (for a long time, when you Googled "django class based views", the first results concerned general views).

Now the documentation is getting much better, and the tools that are easy to understand are great (see ccbv.co.uk or pudb ).

I suggest learning and using Class Based Views for the same reasons that people offer OOP, it reduces code reuse and increases code reuse (inheritance, mixins) ... in other words, it is DRY.

Another thing worth checking out is how other projects use CBV ... one of my last favorites is django-oscar , which uses them for a good effect.

+3
source share

All Articles