Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Register for the site (getting started)
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, it looks like we're making our way through the various subjects over here. Let's open up a new HTML form section.
0:08
This is Chapter 12 in the source. Of course, I've already pre-configured this to load in PyCharm and registered the site
0:16
by running to setup.py, things like that. So let's just see where we are. It's looking good. Our sites are really coming along.
0:23
Alright, quick on logging though. Check out the URL. See, there's nothing here. Click on this. Hash, hash. That's not amazing.
0:32
That means basically this is not implemented. All right, over here in the AccountController we have a little bit of information and I think I had left
0:40
more than I intended to in there before. So we're going to start from this. This allows us to show. Well, it did not actually show or anything
0:49
because we can't get to it. We could come over here and type just /account and it says, "Hey, here's an account." Not a whole lot of details.
0:58
Also notice we're not checking to see that we're logged in. So it could say your account is such and such, right?
1:04
I mean, we'd really come up with no data but still, well we don't want people to get to their account if they're not logged in or have an account.
1:11
We also have over here our templates. We have an account that has a truly bare login page and sort of details, right, the login and register.
1:21
They all just look like this. I guess it's with pointing out. We also have already added some routes for this.
1:27
We do that before. We have /account/login /account/register, /account/logout. Place to start would be register
1:36
because we're going to need an account to login. Let's create it by registry. So we have index and index is really basic.
1:42
It's just basically a static HTML page but let's go and create another section and these dividers become more helpful
1:50
when we're working on these forms. This would be register. And this is really easy to forget. Do not forget to change this.
1:57
If you change like information up here but you don't change the name it's just going to throw away that function
2:05
and redefine it to be, guess what, register. All right, so let's go to register. Now, like what I was just saying
2:11
we actually want to have get post redirect pattern. So how does that look? Well, we're going to come over here and say the method equals GET and POST.
2:27
So this method is going to handle displaying the form. In this case, maybe there's nothing but maybe we pass over data
2:32
like here's all the countries you can pick from or here's, I don't know, right maybe filling out, drop down, and things like that.
2:39
In this one, we're going to actually process their request. But the thing is set about the function names also applies if we just leave it like this.
2:47
There only be one function at the end of this module and when it's loaded and that's the one that only handles POST. So the GET would be 404.
2:54
So come over here and we can do a GET, and a POST. Now let's just make sure everything is hanging together here. Boom, register.
3:05
Register seems to be working. It's all wired up. Okay, so this is the starting structure of our get post redirect pattern.
3:13
We're going to have a form in here. Put our form details in there and then we're going to submit that form. First, I'm going to show the form here
3:23
and we're going to submit and process it here. In ideal situations we technically won't go back to this page.
3:29
They submit it, we process it. We move along somewhere else. But if they get something wrong, if they've if I don't fill out the email address
3:36
well have to have that. We can't register that. So we're going to use the same template to render same details plus an error.
3:44
If things got successful, when we want to sometimes you can meet this render but if you have any possible chance of an error, you can't.
3:51
Now one more thing, this drives me crazy. Over here, notice we're not using request. Similarly here, we put underscore
4:00
but this one we're going to need to use the request. That's where we get the form data from so we're just going to leave it like this.