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