9

I am new to Django and Python and after reading a lot I decided not to use global functions as views (maybe because I am not used to global functions but mostly because I want a better reuse level and prefer to have smaller code blocks). The problem is that most of the Django apps I have been checking do not use a class based approach.

Is this because I am checking code in the wrong places (I started with the Django tutorial and then on github)?

I'm asking this because in the future I hope to be working with more Django programmers and don't want to adopt a non-standard approach at the beginning.

Demian
  • 103
  • 1
  • 4
  • Learn both, because you'll see both. Use CBVs when you can and when it makes sense to (if you're writing 100+ lines of code just to make the CBV work, it probably means it would be more efficient to use a function-based view). When CBVs work, they work *well*, and are far superior, but there's limits to everything, and sometimes it just makes more sense to use a function. – Chris Pratt Jul 20 '12 at 14:52
  • @ChrisPratt thanks, I was thinking to use both strategies. You mention writing 100+ lines of code to make CBV work and I agree but I was thinking what about writing a 100+ lines of code in a view function? I don't have the experience with Django but the template engine seems not very powerful compared with other frameworks so I think I will have to write more code in the controller. – Demian Jul 20 '12 at 19:01
  • 1
    That's not a hard and fast number; I only meant to imply that if you're writing a lot of code to get a CBV to work the way you need it to, you should consider whether it might be more efficient to just go with a function-based view. The template engine is under-powered, intentionally so. Django philosophy says mixing HTML and code is bad and should be avoid as much as humanly and rationally possible. So your view should set up everything the template will need, and then the template ideally just dumps it on the screen. – Chris Pratt Jul 20 '12 at 20:14

1 Answers1

11

One fact is that class based view was added in Django 1.3, so if you're planning to support old Django version than you will have to stick with function view, for example if you want to create a generic app and launch it in GitHub or such for other people to use.

As for when to use class based view over function view i think this will depend on your use case, my advice is that you have to understand the advantage of each way, and IMHO it's pretty normal to have both implementation in a single project.

There was this nice article of Nick Coghlan on response to another great article CBV were a mistake that it worth to check.

Hope this was helpful :)

Community
  • 1
  • 1
mouad
  • 67,571
  • 18
  • 114
  • 106
  • Thanks! it was really helpful and I enjoyed the articles (I think Luke Plant has some very good points but he arrives to an incorrect conclusion because he analyzes an incomplete set of scenarios). Do you think if I use both styles in a project will be problematic for other programmers to work with my code or it is a common scenario? Thanks again! – Demian Jul 20 '12 at 18:44
  • @Demian: You're very welcome, as for your question i think that if someone want to use Django (>=1.3) he should learn both styles (so should your teammates), and if you're using the two styles (CBV vs FBV) in your project (which IMHO is a good practice), i think what can be problematic is to explain why did you choose to use X over Y, so you should have a good reason to give to them :) by understanding advantage of each one over the other. – mouad Jul 20 '12 at 23:32
  • Good to say that the last article mentioned was updated and the author does not believe CBV are always bad. In fact, he acknoledges there are cases CVB shines. :) – MatheusJardimB Sep 27 '16 at 12:04