ListView and DetailView6:48 with Kenneth Love
Most web apps end up needing ways to see a list of objects and a single object. Django's `ListView` and `DetailView` CBVs make these two common needs a matter of extending a class and setting a model.
Okay, so basic views and templates are one thing. 0:00 But what about when you need to deal with models? 0:03 Most of the time, once you have a model, you'll want to show a list of records and 0:06 eventually a single record. 0:09 I know that's usually really high on my list. 0:11 Django provides two class-based views for exactly these two scenarios. 0:14 Like most of the other CBVs, they're creatively named, ListView and DetailView. 0:18 They work very similarly to each other, taking a model to use for 0:22 looking up their records. 0:25 The DetailView expects a URL argument of either slug or pk though. 0:26 As part of their genericness, they automatically create some context 0:30 variables and have predefined template names. 0:33 Both of those can be changed though. 0:36 Time to hop into workspaces. 0:38 Over here in the teams app, in the views.py file, 0:40 there are already two views. 0:44 One shows a list of teams, and the other shows a single individual team. 0:47 Now I want to replace both of these with their class-based equivalents. 0:53 So just like before, the first thing I need to do is import the view classes from 0:56 django.views.generic import ListView and DetailView. 1:02 Fairly obviously named, I think. 1:10 I think I'm gonna start with the ListView. 1:13 Now, these really aren't much harder than the template view that I 1:16 did in the last video. 1:20 So I'll do it down here, I'll do class TeamListView. 1:21 Again, I just like having that kind of naming scheme. 1:25 You don't have to do that yourself. 1:28 And this extends ListView. 1:31 And one of things that you'd have to set on list views and detail views and other 1:33 views is you either have to provide a query set, or you have to provide a model. 1:37 And so I'm gonna provide a model, which will be models.Team, all right? 1:43 And believe it or not, that's it. 1:49 So let me go change the URL. 1:53 So right now in the URL, we're using views.team_list. 1:56 And that's the one I want to update. 2:00 So instead of team_list, TeamListView.as_view. 2:02 And I'm gonna save that, all right? 2:08 So if I go back over here and look at my Teams page, No teams are showing up. 2:12 Well, okay, let's check the template then. 2:21 So I'm gonna come over here to my templates. 2:24 And I'm gonna look inside teams. 2:26 And I'm gonna look at, this a little wider here, team_list.html. 2:28 Okay, so scrolling down through here, 2:36 looking through this stuff, I mean, it looks okay. 2:39 You know what, let me check the documentation, cuz here I'm doing this for 2:43 team in teams. 2:46 And I'm pretty sure I have this thing called teams, but 2:47 let me check the documentation. 2:50 I just so happen to have a link over here. 2:52 All right, so let me, let's scroll down through here. 2:57 We've got extending things, generic lists. 3:02 Blah, blah, blah, friendly template context. 3:07 So here's something about the the names and adding extra context. 3:10 Let's look at this. 3:18 So it says here 3:19 that I get this thing called object_list that has all of the objects in it. 3:23 Okay, so cool, I get object_list, I like object_list, that's a good thing to have. 3:28 And I see that if I was using a thing called publisher, 3:32 I would get this publisher_list. 3:36 So cool, so that means I should have a team_list. 3:39 So I should be able to come over here. 3:44 And instead of for team in teams, I should be able to do for 3:45 team in team_list, and get the information. 3:49 Let me refresh this. 3:52 There's the teams, all right. 3:56 Now, both of those names that I just talked about, object_list and 3:58 team_list, are really good choices, I think. 4:03 But I don't want to change the templates too much, right? 4:06 I've got a teammate who's working on design in front end. 4:10 I don't wanna change the templates on them. 4:14 I want them to get the templates that they expect to get. 4:16 So I'm gonna change that back to teams. 4:19 Now luckily, I can change the name of the variables in the context. 4:21 And I don't even have to override get_context_data for this. 4:25 I can just tell Django, hey, use a new name. 4:28 It's actually an attribute. 4:32 And if you look here, you might've seen, this thing here. 4:33 I'm gonna do that. 4:39 So here in my view, I'm gonna add a new attribute, which is context_object_name. 4:40 So what name should Django give to this thing in the context? 4:47 And I want to use the name, teams, all right? 4:52 So if I refresh this, I should still see the teams. 4:56 There they are. 5:01 Now, due to the CSS that I'm using, we get different colors, but there they are. 5:02 Okay, so that's awesome, got what I was looking for. 5:05 All right, now I wanna do this DetailView. 5:09 Need to override that one. 5:12 Let's do another line or two there. 5:19 Okay, so class TeamDetailView, 5:21 cuz that's the way I name things, DetailView. 5:24 model = models.Team, just like before, all done. 5:29 All right, now urls.py, I need to change this one. 5:35 I don't want team_detail, I want TeamDetailView.as_view. 5:38 Save that, and by clicking on one of these teams, I should get to see the details. 5:44 Let's check out that Raptors again. 5:50 There’s the details, all right. 5:53 You'll probably find yourself using ListView and DetailView for 5:56 nearly every model you create. 5:59 They're fairly indispensable for building CMSs, stores, and many other sites. 6:00 Did you notice that I didn't have to change or set any template names? 6:05 And that pk argument URL just worked. 6:08 I'll admit I cheated a little bit there and set the names correctly to begin with. 6:11 This feature alone will save you so much time. 6:14 All of the generic views that deal with models and 6:17 forms, so most of the ones that I'll be showing you in this course, 6:19 have default template names, context variables, and URL arguments. 6:22 You can customize all of these, of course. 6:26 But if you use the defaults, you save yourself even more time. 6:28 Check the docs, which I've linked in the teacher's’ notes, for 6:31 all of these nitty-gritty details. 6:33 Most of them are pretty obvious though. 6:35 So you'll probably memorize them pretty quickly. 6:37 But now, after getting those list and detail views, 6:39 you'd probably want to create, update, and delete model instances, too? 6:42 Let me show you those three views in the next video. 6:46
You need to sign up for Treehouse in order to download course files.Sign up