#100DaysOfWeb in Python Transcripts
Chapter: Days 37-40: Introduction to Pyramid framework
Lecture: Paying a bill: Submitting forms

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next thing we should do one of the final things we're going to do is to submit that form let the users pay part of their or all of their bill
0:09 and then show them the updated details. And to do this, we're going to use our details_get and our details_post.
0:15 We already have this working, this just shows the state of a bill as it is. This one we have not written yet but we're going to follow what is called
0:24 the Get Post Redirect pattern. So, we're going to show the data, show the form that's the get and then they're going to let them post the form
0:33 that's what this method does and then if everything is good, we're going to redirect them to a new view of the data.
0:39 Either back to this page or back to the summary information probably back to that page. So, how do we do that?
0:47 Well, let's start by taking the same data we're doing here because we're going to need a lot of the same stuff like the bill_id and what not.
0:56 So we're getting the bill_id, this is part of the URL that doesn't exist, we're going to bail, we get the user
1:02 and we also going to need to get the amount. How do we do that? Well, we again go back to the request and we're not going to use the match dict
1:09 that's for the routing. We're going to use the post dictionary. Then we get the amount and I'm going to pass it and maybe -1 if it doesn't exist.
1:19 And again, error handling here would be a good idea. We're going to skip it. So we'll say it's something like this
1:23 if amount < 0 or amount > bill.total-bill.paid: So they're trying to pay too much or they're trying to pay a negative amount
1:36 Set some kind of error, error equals nothing. We'll say: You're amount must be more than zero and less than what you owe.
1:48 We can do something better than that but if we have that we want to return this error here, like so. Now if there's not an error
2:00 actually let's do like this, I guess. If there is not an error, we would actually like not to just show them this. Just send them back
2:09 somewhere else. So we're going to return our response. Let's do it this way, raise HTTPFound exception.
2:19 I'll say the location is slash, details, sorry no that's bill and here we format with the bill_id. So, let's just try that, we are not actually doing
2:31 the payment yet, there is still left to do let's just see there is something happening. So, if I go over here and I say we are going to make a
2:36 payment of $100 or something like that. We'll come back and say; oh, we caught this error amount.
2:45 Oh, I see we are making a mistake here, we are going to have to pass one more thing. Let's go to details.
2:52 Now notice, if there is an error we want to pass along this amount, so we are going to need to return what they
2:59 said they'd pass. That's because we want to keep the form the way they had entered it. So, over here, want to make sure we pass this amount.
3:08 Let's try our validation again, we'll go back here look at it, pay $100. Notice there is an error, it says; Your amount must be
3:22 more than zero less than what you owe. Fix that, spelling your amount, right there. So how do we get that to happen?
3:29 Let's look over here at the details. There is one more cool thing we haven't done yet. Check this out. So here is the error and it is red
3:36 because it has a class error. We said TAL condition. So this entire block of code, HTML is only showed if error
3:43 is truthy in Python. So, a non-empty string. So, that only shows up if there is an error. Obviously, the value of that
3:51 input we want to carry that amount around so that we can keep that there and only give them the error.
3:55 So, let's say we want to pay $100, which should be valid. Go back here, everything is good. If we want to pay
4:02 $10,000, it's going to be an error again, the amount you owe must be positive. It's kind of annoying this is below the fold, you like them
4:09 to maybe see that but that's the error handling we've gotten. Final, final thing, super quick, let's just do a payment.
4:16 And the repository has an add payment for, you go to the amount and they bill, the bill_id. So, let's see if we can pay off some of these bills here.
4:27 Alright, I suppose we got some money, we going to pay off this gardening bill. We are going to come over here and we
4:32 are going to start by trying to pay $100. $199, that's not going to work. Oh yeah right I would like to pay $100, so this amount should just be
4:41 seventeen, hey look at that, seventeen, got some more money I'm going to pay off fifteen, now we are down to 2.
4:48 Now, if we do this it goes to zero and also we put the same TAL condition on the same form so say if it's paid off
5:00 don't show that form at all so they can't try to pay on stuff they don't owe or whatever, so it's gone.
5:07 We go back here, our gardening is no longer up here notice the gardening is gone, now the gardening is right
5:12 here as paid off. Awesome, right? How do we do that? Get, Post, Redirect. We have the get the details
5:21 here is our get request method, just show the details. Down here we are saying is the post one that handles
5:29 the submission of the form or getting the data this one from the form, if everything is validated we record the action in the database and we do the
5:39 redirect by raising HTTPFound to the URL we would like them to go to. That's it, that's how we accept your input and let
5:46 them interact with this data, nice right?


Talk Python's Mastodon Michael Kennedy's Mastodon