Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Franken-Views6:30 with Kenneth Love
So what about when you want a list of records with a form for adding a new item? Maybe you can just smash `ListView` and `CreateView` together?
This approach has gotten a lot better in recent versions of Django. It used to be a nightmare but now it often just works. I can't tell you how glad I am for that change!
It still isn't the best way to combine view functionality, though. Generally you'll want to combine view classes with mixins to get just the bits and pieces you need. Check out the next video for how to do that.
Sometimes overwriting methods just isn't enough. 0:00 You need to show a model detail and a form for editing that model in the same page 0:02 and you don't want people to just read the data in the form. 0:06 So what do you do? 0:09 Well, the first solution most people try is just throwing a form into the context. 0:12 That works but then you have to reimplement a lot of form view or 0:15 it's children in the detailView and yeah, 0:18 frameworks shouldn't make you do more work but Python classes are pretty malleable. 0:21 Maybe you could just combine the two classes together to make a new 0:25 Franken class. 0:28 This can actually work better than my introduction might have 0:31 led you to believe. 0:33 At least, that is if you combine just the right view classes in 0:35 just the right order. 0:38 More often than not though, 0:40 you're going to want some combination that's, you know, full of headaches. 0:41 So, to start off, let me show you one that's not terrible. 0:45 So, I'm going to come up here to the detail view, and 0:48 I want the detail view to be able to like update a record. 0:54 So, detail view, update view. 0:58 I have to set my fields, so fields is equal 1:02 to name practice location and coach. 1:08 The models are gonna be models.team and 1:13 the template name is going to be teams/team_detail.html. 1:17 Cuz that's the template I already had set up, that's the template I want to use. 1:23 Okay, and I wanna make sure that it's using it. 1:27 So now I'm gonna come over here to team_form and 1:29 I'm gonna copy the form here because I don't wanna have to type it again, right? 1:32 All right. 1:38 And then in team detail, before this update <hr> I wanna put in a new <hr>. 1:39 Actually, I'm gonna put a if user is equal to team.coach, 1:45 cuz user is the user who's currently requesting the page. 1:52 So if that's the case then I'll put in my form and we should be good to go. 1:59 So if I come back over here and I select the Tigers and so 2:06 now we can see, here's the form and I can update this. 2:11 So I could be like Tiger Tamers because they changed their name. 2:14 Save. There you go and 2:21 now they're called the Tiger Tamers. 2:23 So, awesome. 2:26 So it worked. 2:27 It did exactly what we wanted, but when I asked you to first join me in work spaces, 2:28 I said it doesn't always go super smoothly. 2:33 So, I think it's a great idea to be on this page and be able to 2:36 create a new team straight from this page, right, at least, for authenticated users. 2:44 I probably don't want just any random person doing this and 2:48 I would probably actually move this into a modal or something like that, but for 2:52 now I'm just going to get it on the page and my UIUX designer 2:56 teammate can figure out the best interface for it later. 3:01 Okay, so back over here in views.py and and here on this list view, 3:05 I'm going to add in the create view and I mean, this worked before. 3:13 So give it a try. 3:19 So context object I don't want to change. 3:21 Fields, I think I've typed that like four times now so I'm going to copy and paste. 3:23 And template name is going to be team'steamlist.html. 3:29 There I go, okay. 3:34 And then of course I need to add the form into the template. 3:37 So here we go. 3:43 We've got our teams there. 3:44 Let's add a new thing here and 3:47 I'll say if user.is_authenticated, 3:50 then I'll toss in an HR, toss in the form and okay. 3:56 Nothing special there, all right, so, should be ready to go, 4:04 should be able to just refresh this page and get what I want. 4:08 And I have an AttributeError. 4:11 TeamListView object has no attribute object, what is this about? 4:13 I actually know what's up with this one but 4:19 this is a really good illustration of why you often have more problems than you 4:21 expect when you're combining class based views. 4:24 So if we look at our views.py, we see that we have a list view and creative view. 4:27 We are extending both of those. 4:32 But we're extending list view first and then we're extending create view. 4:34 This means that create view is the final class that it extends. 4:38 And anytime we've done a super, which the mixins for 4:42 class based views do supers all the time, supers work from left to right. 4:45 So the list view does all of its stuff and then the create view does it's work. 4:50 Since creative view's build process look for an object attribute and 4:53 list view doesn't create that, because it doesn't have a single object, 4:57 it has multiple objects, it doesn't exist. 5:00 The attribute doesn't exist and we get that error. 5:04 Thankfully, this exact problem has an easy solution, just reverse the order. 5:07 So, CreateView then ListView, okay? 5:15 Everything else stays exactly the same. 5:19 If I refresh this, Then I get a template error. 5:22 Cuz I forgot to close that template. 5:29 All right. 5:32 There, so now I have the field. 5:34 So I can create a new team here if I want. 5:36 I can do the Adders and they practice at the old field and 5:40 test user is their coach because I'm tired of coaching so many teams. 5:46 And then we go, we have the Adders, they have shown up. 5:50 So that's pretty awesome. 5:52 This now works because create view's get method gets fired first and 5:54 that method actually creates the blank object attribute. 5:59 Later, list view runs on its own, doesn't do anything to that object attribute and 6:01 everything's fine. 6:06 So yay for execution order. 6:07 As you might have guessed, this isn't the best way to get more custom views. 6:09 Django's class based views are mostly built up using mix-ins and 6:14 those mix-ins are available for you to use. 6:17 Take a break. 6:19 Take a walk, get a snack, tell someone they're important to you. 6:20 Then come back to the next video, where I'll walk you through using Django's 6:23 mix-ins, some third party ones and how you could even write your own. 6:26
You need to sign up for Treehouse in order to download course files.Sign up