What are Class-based Views?2:22 with Kenneth Love
Most people start writing Django views by using functions. This works great until you start creating multiple features that share a lot of similar view functionality. Function-based views are hard to reuse or make abstract. Django provides several classes for creating generic views, though!
Hi, I'm Kenneth, and I'm a Python teacher here at Treehouse. 0:04 Thanks for joining me for 0:08 this course about a small but very useful feature of Django. 0:08 Way back, long ago before I was a teacher, I was a full-time Django developer. 0:12 One of the biggest projects I worked in, at least in amount of code, was a CMS for 0:16 managing apartment websites. 0:20 While that product had many different areas like amenities, community maps, and 0:22 unit tracking, each of those areas had very similar functionality, creating and 0:25 deleting records, for example. 0:29 In regular old Python when you find yourself doing the same work over and 0:31 over with just light changes you put your code into logical classes 0:34 expose some configuration variables and save yourself a lot of work. 0:38 But what if you find yourself doing that in Django, 0:41 specifically were views are concerned. 0:44 Well you saw the title of this course, so 0:46 you probably guessed that you make them into classes. 0:48 Django two types of class based views. 0:51 There's a class that's just named view that you can use to build 0:54 class based views. 0:57 You can customize this class to your heart's content and 0:58 make it do all sorts of amazing things. 1:00 There are also a set of views for common use cases that are called generic views. 1:02 These generic views handle all of the normal cred operations, creating, 1:06 reading, updating and deleting records. 1:10 They also handle things like displaying and validating forms, 1:12 finding records by date, or automatically performing HTTP redirects. 1:15 All of the generic views are built on top of that basic view class too. 1:19 So back to my story about the apartment CMS. 1:24 We switched over to using class based views and 1:27 found that we could create new features in just a fraction of the time. 1:29 Things became easier to test too, 1:32 because we could test only the special parts of each app. 1:33 And review instead of having to check lots of different factors each time. 1:36 Of course, this shift wasn't free, there's a time and mental cost for 1:40 changing how you build something as numerous as Django. 1:43 And Django's class based view inheritance tree isn't the simplest tree ever. 1:46 Still though, I feel it's a change well worth the cost and 1:50 I haven't built any Django project without class based views in years. 1:53 Generic views get me 85% or more of the way to what ever functionality I need and 1:56 then I can bring in reasonable customizations to round out that last 15%. 2:01 In this course, I want to introduce you to view and some of the more common and 2:06 useful views that are built on top of it. 2:09 I also want to show you how to customize the views with small classes 2:11 in this mix-ins to tweak how your views work so 2:13 you get exactly the functionality you need. 2:16 Settle in, get your learning had on and come one about Django class based with me. 2:18
You need to sign up for Treehouse in order to download course files.Sign up