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