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