The View Class7:27 with Kenneth Love
The most basic class for creating views is named, appropriately, `View`.
View class is the basis for all of the other Django class-based view classes. For the most part, it's a remarkably simple class with only a few methods. You can take a look at the class if you're curious.
Also, all CBV's use their
.as_view() method in URLs, so get used to typing that. You can turn that into a variable (
my_view = MyView.as_view()) and then link that in your URL but that is often seen as redundant and pointless.
Jin goes class base views come into flavors, 0:00 a generic class base views and well, the few classes they all extend. 0:03 Okay so, a single class might not count as a flavor. 0:07 While the view class might not be the simplest to use, 0:11 its moment the least amount of special bits to understand. 0:14 So I think it's a good place to start. 0:16 I've created an app for tracking kids soccer teams. 0:18 I'll be working on this out throughout the course. 0:20 So, let me help over the work spaces, show you the app, and 0:22 start in on the view class. 0:25 So first, let me walk you through this project. 0:27 It's a pretty simple project. 0:29 It tracks teams and players for like a kids soccer league. 0:31 You could use it for adults soccer leagues too but my idea was it was for kids. 0:35 So I've got everything set up here in the admin, so let me hop over here to slash 0:40 admin, and I log in. 0:46 The username and password by the way should all ready be stored in 0:51 the database, and they are listed here in this read me file. 0:55 So you can log in using those credentials if you want. 1:01 So here in the admin, we can add teams and we can add players, 1:05 all those kinds of stuff so like I'm gonna add a new team, 1:10 I could add say the Raptors, and I'll be their coach, 1:12 sure, and we're gonna be at the Downtown Gym. 1:17 And you can see here, I can add players, pretty easily too, 1:22 if I wanted to add Andrew's a player and 1:25 he's 10 and he's gonna play Wide midfield. 1:29 And Lacey is a player and she's 9 and she's gonna play Sweeper. 1:36 So to say that, and now get a second team. 1:43 There are a couple of front facing pages as well. 1:47 Actually, let's just take this off. 1:50 So this is the home page is just rendering a template. 1:54 If I go to slash teams, you can see a couple of teams here. 1:57 And if I go look at the Raptors, 2:02 this is the team I just created, there's all their information. 2:03 So yeah, site works, you can ignore all of this stuff. 2:07 I just wanted to give you the idea of a site in the middle of building a site. 2:11 Not everything works, most things don't, but 2:18 you can feel free to add whatever features you wanna add here. 2:20 Now though, let me show you some of what's actually here in the file. 2:25 So if I go here in the Djangoal, the stub, and 2:28 I go look in the views.py, you see that there is a view here. 2:34 It's just rendering a template. 2:37 Like literally, it does nothing else, not even a context dictionary, so 2:38 I need just rendering that one template. 2:42 This would be a good view to use to show you the template view, but 2:45 I think I can wait to do that till the next video. 2:49 For now though, 2:52 I wanna show you how to make just a simple view that just sends out the word hello. 2:53 Like you do for every new thing you learn, you're supposed to say hello world. 2:58 So I'm gonna do that here in this views class. 3:02 So the first thing I need to do is, I need to import the view class. 3:04 So, from django.views.generic import View, all right. 3:09 And then, I need to make a new class that extends this view class. 3:17 So, class HelloWorldView, and it's a (View):. 3:22 Now this is as name is a 100% optional, you can name as whatever you want. 3:27 I just like using things, you'll see later on if I do a create view, 3:31 I was put in like create or create view in the name, just so that I know what it is. 3:36 Now each view with lowercase v, that's based on a view with a capital V, 3:41 automatically accepts the HTTP git method. 3:47 To handle that kind of request though, you need to create the get method on the class 3:50 and I need to return an HTTP response. 3:55 So I should probably import that too. 3:57 So let's do that, from django.http import HttpResponse, all right. 4:01 And then, back down here in our HelloWorldView. 4:07 I’m gonna put in dev get(self, request):. 4:10 Takes two arguments, self because it's an instance and 4:14 request because it gets a request, just like function based view div. 4:17 And I'm gonna return the HttpResponse, and 4:21 I'm just gonna return the string ("Hello World"). 4:25 And let's put an exclamation mark in there cuz you should have exclamation marks. 4:32 That's it, that's the entire view. 4:36 But having a view doesn't do me any good without having the URL, right? 4:38 So URL's for class based views are just a little bit different than they are for 4:43 function based views. 4:48 Since this view is in my global, for lack of a better word views, 4:49 I'm gonna add the URL to the global URL's file. 4:54 So this one out here that Django automatically creates for me. 4:58 I've all ready imported views because I wanted that home view, right? 5:01 Okay so, let's go ahead and let me go ahead and add this right here, and 5:07 I'm gonna do (r'^hello/$', and then just like with function based views, 5:12 I'm going to add the name of the view, so HelloWorldView. 5:19 But I can't just point to a class, I need to point to a method or 5:24 a function because that's how URL's work, they have to point to a callable, 5:29 and classes by themselves are callable. 5:33 So I'll point to this as viewClass method, 5:35 sorry, it doesn't transform the class into a method but 5:41 it handles instantiating an instance of the class and doing all of the responsive 5:47 work that needs to be done to turn the class into a function. 5:51 Okay, and then I'll give it the name = 'hello'. 5:56 All right, so save that file. 6:00 Save everything. 6:01 So, I want to talk a little bit more about what this does really quick. 6:03 So we call this method this as view, and it's a class method so 6:07 that means it doesn't need an instance of the class to run. 6:12 It creates an instance of the class, sets up some different things like the request 6:15 object and all that stuff, and then it runs the classes dispatch method. 6:19 And the dispatch method is the method that's responsible for 6:23 running the correct method based on the HTTP request. 6:26 So in this case, 6:30 when I make a get request, the dispatch method will run the get method. 6:30 Now, knowing all the bits and pieces of the stance isn't really important though, 6:35 unless or maybe until you're gonna start messing around with dispatch on a view. 6:39 That is required sometimes though so just kind of keep in mind that dispatch is 6:45 responsible for handling HTTP methods appropriately, okay. 6:49 So view created, URL created, and pointed to the views as view method. 6:54 Time to test it up. 6:59 So pop over here and instead of teams two, I'm gonna go to hello. 7:01 There's my hello world and it works. 7:06 That's awesome. 7:07 The view class is a great resource we needed to control everything and 7:09 starts at the bottom of class based views. 7:12 If you like me though, you won't find yourself using it very often. 7:15 I've always found more use in the generic views that are aimed to solving 7:18 specific problems. 7:20 So let me leave the low level behind, move a bit further up the path, and 7:22 get to the first generic view. 7:25
You need to sign up for Treehouse in order to download course files.Sign up