Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: The account management routes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's go ahead
0:01 and round out most of the other routes and structures.
0:03 So that we can just build them up over time.
0:05 In our application
0:06 we're going to be able to view packages
0:08 we're going to be able to go the homepage
0:10 and we're going to have an account.
0:12 There's actually a couple other things we'll add
0:13 but let's just focus on those for now.
0:15 So let's go in and add a Python file
0:17 called account_views.
0:21 Now there's a bunch here
0:23 I'm just going to copy them over
0:24 and then we'll talk through them
0:26 'cause you've seen me write plenty of MDV methods so far.
0:30 So, let's go up here and look. So again
0:33 we're using blueprints, this one is called account.
0:35 Now we're going to have a couple things
0:37 you can go to your account page
0:39 and just view details about your account.
0:41 It's going to be not super interesting
0:42 I'll just say
0:43 basically you're logged in as so-and-so
0:44 but we're going to show that.
0:45 We need to go to the database
0:47 and pull your account out
0:48 things like that.
0:49 So for now we're just going to have it empty
0:51 it's going to be /account
0:52 that makes a lot of sense.
0:54 Register is more interesting
0:55 how do you get an account?
0:56 You register on the website
0:58 this gets us to a really interesting design pattern
1:01 that we're going to talk about more later.
1:03 But I find to be quite helpful
1:06 and a lot of times, I see people doing it suboptimally
1:09 let's say
1:10 I'm not sure I'm going to call it entirely wrong
1:12 but it's definitely suboptimal. And that is
1:14 I need to show a form
1:16 initially I need to get that form in front of the user
1:19 so I'm going to have an HTML static page
1:21 and show it to them.
1:22 But then I also need to process
1:24 their submission of that form.
1:26 Those are very different things
1:28 those are not usually even closely related to each other.
1:31 Often though some people will write one function
1:34 register and check the method on their request.
1:37 If it's GET, do this. If it's POST, do that.
1:39 And they really should have two separate functions
1:40 and the routing lets us do that automatically.
1:43 So what we can do here
1:44 is we can say account/register
1:47 and then only handle GET requests on this method.
1:52 Over here account/register.
1:54 Same URL but only handle POST methods here
1:57 and usually we won't even use the template
1:59 in this response
2:00 we'll do some kind of redirect in the end
2:02 unless there's an error like
2:03 "Oh you tried to register an account that already exists".
2:06 So we need to like pass that back to that template.
2:10 Also you'll note
2:11 that we have to call these different things
2:13 register_get and register_post is what I like to use
2:15 but they can be whatever all right?
2:17 They're just Python functions
2:18 they're not really involved on the user side of things.
2:21 Here we're going to figure out what details we have to show
2:23 that could be none, it could be just like this
2:25 or it could be more. Here we're going to process it
2:27 validate it and so on. Login is exactly the same
2:30 login_get shows the form
2:32 login_post checks to see if they're there
2:33 and redirects them to their account
2:35 or wherever they're trying to go.
2:36 Logout pretty straight forward
2:38 that's just going to log you out
2:39 and redirect you to the homepage
2:41 or something to that effect.
2:43 So here we have our account_views
2:45 and the most important takeaway here is
2:48 filtering the request out by the various HTTP verbs
2:52 one for GET, one for POST.
2:54 Most important when you're submitting forms
2:56 and doing that kind of user input.