Building Data-Driven Web Apps with Flask 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
Now in this next section what we're going to do is we're going to create a login form and a registration form that will let us register for the site
0:08
and then later log into it. So we would have user management in our web application as well as these forms.
0:15
So we're going to put these two things together and actually tackle them at once. So if we run our app, you'll see over here it's running right here.
0:22
And if we try to click on register or login we're going to get a sub-standard experience. As in, error, URL was not found.
0:31
However, we actually did put a little bit of work in here but what we haven't done yet, is we have not yet gone and imported the blueprint.
0:38
Remember for each one of these view sections we got to import their blueprint so we're going to go and get the blueprint out of here.
0:46
Alright so now if we refresh that now we just get, hooray, there's no template. Okay, well, it's a little bit better.
0:52
What we need to do is go over here our template section and we need to create a new directory called account.
1:01
Remember that's how we're organizing stuff. all the operations inside account views are working with templates in here
1:09
and honestly, the easiest way to just create another page is to copy exactly what we have and about as, about as simple as it comes.
1:16
So let's go and call this register maybe you want to login, as well. Now, let's just see if we got this working.
1:30
We refresh. Alright. This is about our site well not really, but the registry thing is working so that's a good bit right here.
1:38
So this will be Register at PyPI This will be our form. I'm actually going to use this little CSS section as well so this is pretty good.
1:52
If we look over in the account view here the account view methods. We actually already put the get/post/redirect pattern
2:00
well at least the POST and GET part in place. So we've got our index, that just returns. Here's your account, there's no data access.
2:07
There's no data exchange at all. This is just, you know the skeleton pieces but it's here. So this is going to be, I want to see my home page.
2:15
Maybe we send them here when they log in. Here's the login and we going to do a /account, /login for both these methods
2:24
but this one, here's the important distinguishing part inside Flask for the get/post/redirect pattern. This one only responds to GET requests.
2:33
This one only responds to POST requests. A lot of times what you'll see is people will just have like a login function and they'll say
2:40
if the request, you know Flask request is GET sort of thing. We'll do something else, we're going to do something else. This is so bad folks.
2:47
It's really, really not good to like have these branching statements and these functions doing two things. So we can just use this little methods
2:56
but here to determine which function gets run and when you're doing this make sure that this one is called _get and this one's called _post.
3:04
They don't have to match the HTTP verbs but what they do have to do is be distinct. You can't have both two login functions
3:11
that will be a RuntimeError inside Flask.