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