#100DaysOfWeb in Python Transcripts
Chapter: Days 37-40: Introduction to Pyramid framework
Lecture: Adding the details route

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The last major feature of our site is to view the details of a bill and then pay off either some or all of that bill and that will update our database
0:10 update our homepage, and so on. So, what we want to do is add a details method here. So, it's going to be really similar to this
0:18 so let's start by copying and pasting. So be super careful to change that name because if you don't change the function name
0:26 it's just going to replace it, it's not going to be an error. It's going to have details and we're also going to get the details
0:34 show a form and let them potentially submit it. So this one is going to be a get, and there's going to be a post as in posting the form back, as well.
0:43 We're going to need the request here, so we'll pull that out. Along here, let's go ahead and do the post one as well.
0:50 Post and the route name is going to be the same. We'll just call this details. And this is also hopefully going to match up to be details.
1:01 So how does pyramid select from this one to that one. Does it use the function name? I guess it could, but no, it does not.
1:08 You have to go up here and explicitly say the request method is a get, if you can spell. And this one the request method is going to be post.
1:22 All right, we're looking pretty good here. We're not going to worry about the post one for a while. We're going to just work on this one.
1:29 The next thing we need to do is make sure we have a route named that. If we try to run it now, it's going to crash and say whoa whoa whoa.
1:35 We went looking for a route called details and it doesn't exist. So we can't wire up that thing to Pyramid. So we come back down here to our routes
1:45 and down here we're going to say, we have a details and this one is going to go to bill remember it's like bill/123
1:53 Now this 123 is really important here This one, two, three is what's passed as data to this function, it's not just something static.
1:59 So how do we indicate that? We go over here and we put a curly and we say bill_id. Call it whatever you want. But I'm going to call it bill_id.
2:08 That way we can go and grab this bill_id from there. So to round this out, we don't care about the user_id right now, we want the bill_id.
2:19 And how do we get it? We go to the request, what's called a match dict and we get the bill_id. Now that comes back as a string, right?
2:28 So we're going to convert that to an integer. Now, you probably wanted to put some error handling around this, but for now, we're just going to roll
2:35 with it like this. All right, and let's just go and put... Now actually, let's just go grab it from the repository.
2:43 So we'll be able to grab the repository and we can get a bill by bill_id. Imagine that! And we'll have the bill is here.
2:52 And let's put the bill like this and I guess we may still want the user so we're going to pass them along.
3:02 We could always use the bills.user, to go in reverse. So I'm not sure it's really necessary, but what you'd probably want to do is you'd probably get
3:10 the user from the cookie and get the bill and verify the bill belongs to that user so they're not just randomly punching in stuff here.
3:16 So you want to get them separately or at least somehow verify that that user and that bill are related
3:23 or you would get some sort of leak just by people typing in ids up there, right? We probably also want to check that this bill exists.
3:29 So we'll say if not bill, right, come back empty if they passed in an id that doesn't exist. We get a return a response from Pyramid.
3:38 Say the status is 404. Something to that effect. Okay, well, we're close to running it. It should start now we've defined that URL.
3:49 If we try to go to it, we're going to find a problem. Missing template. All right, there's no template called, well, home details. We better fix that.
3:59 I'll say details. I'm just going to copy this. It's probably the easiest way. And then, we'll just rip out all the guts.
4:07 So we're going to use that template we're going to punch in the contents. So what we're going to put there is what we're going to show for the details.
4:13 So again, just for design, saving the time around design let me just grab this and throw it in there. Now see, it's kind of involved.
4:21 So, here we have a bill, details, with our little header. I have some styles on that. And here we have a bootstrap grid, so it's going to have
4:29 the ability to go back this cool little back arrow thing we grabbed. It's going to tell you the bills, what it's for
4:37 and then all the details about creating payments scroll down a little bit. Here's the interesting part.
4:42 We have a form goes back to double quote, empty quote empty string so that just posts back to the same URL.
4:49 And it's going to do a post, and it has some text which is named amount. And it has a button that submits it.
4:56 All right, let's see what that looks like. So you go refresh this. Oh, we're not passing along an error. And see what that's about in a little bit.
5:04 But let's go ahead and just pass along None for now. When we get to this processing the input if the amount is like negative or something
5:13 we might want to say "No, no, you can't post a negative amount." So there we go. Here's our bill details, and this is a bill for kids.
5:22 This is a weird error that's starting coming up in the latest version of Chameleon. Let me fix that real quick.
5:30 All right, here we go. We've got our bill details, using the same hero image. Of course we could pick different ones.
5:35 Here's our back button that takes us back. Click on this. Which one did we have? Kids, that one. So it was on this date.
5:41 This is the total bill amount, here's how much we still owe. And we could make a payment by entering some numbers here
5:47 right, like we could pay how much we owe. We could pay 300, have only 14 left. Course that doesn't work yet, because we're posting
5:56 back to that unwritten method that we're going to deal with next. But here's the details, right?
6:00 We go look at another one. Let me go pick this one for games. There's the bill for games. Beautiful, right? Works really well.


Talk Python's Mastodon Michael Kennedy's Mastodon