I’m coming back to Django after a brief encounter with version 1.2, and now in version 1.3 the favored approach to views seems to be using classes.
Keeping in mind code style, maintainability and modularity: when should I use classes, and when functions? Should I always extend from generic class views (there seems to be no harm in using TemplateView), or should I use my own view callable objects?
Thanks in advance.
There are in my opinion two cases for necessity of class-based(-generic)-views:
For anything else use what you feel most comfortable with. As you said you basically good to go with extending from
TemplateViewand overwriting respective methods, though you could also use a function-based approach (and have to deal with render template calls by yourself). That’s up to you in the end.EDIT: One other advantage of class based views is that it lets you separate your code according to the
request.methodin a more cleaner manner and even returns405 Method Not Allowedresponse codes when the wrong method is used. So you don’t have to deal with lines likeif request.method=='POST'orif request.method=='GET'at all and just extend apostorgetmethod.