G
G
German Jet2016-07-04 10:36:48
Django
German Jet, 2016-07-04 10:36:48

Django: CBV or functions?

Are there any generally accepted rules for writing views?
Wouldn't it be bad manners if there are both classes and functions in one view?
Classes are convenient, for example, ListView or DetailView, but to add additional data to the context,
you have to write several get_context_data lines, and you get more code than in a regular function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2016-07-04
@GeraJet

I'm not that experienced with Django, but I'm willing to share what information I have. You might also find it useful...
There are three people (two of them married couples) that I listen to first and foremost on this issue (Django development), carefully and slowly studying the relevant printed materials...
These are Daniel & Audrey Roy Greenfeld , authors of the notorious Two Scoops of Django and Arun Ravindran , developer member of the Django Software Foundation and author of Django Design Patterns and Best Practices .
Both the former and the latter prefer CBV. But this is mostly personal preference. The Greenfields themselves note that they know many of their colleagues who prefer working with FBV (I quote: "...Some developers prefer to err on the side of using FBVs for most views and CBVs only for views that need to be subclassed. That strategy is fine well...." ). In short, it's really a matter of personal preference and the nature of the product being developed.
The only categorical advice from them is not to use CBV to write error handlers ( "...Don't use CBVs to write custom 403, 404, and 500 error handlers. Use FBVs instead...." )
you can be curious about the graphic prompt from Greenfields for choosing preferences depending on the tasks you are facing.

N
Nikolai, 2016-07-04
@evikbook

Better than CBV, they get less copy-paste (and ideally 0), more productivity and efficiency from each line of code. The only thing is to set aside a day to reconfigure your brains for them (make a mini project)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question