Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Routing visualized
0:00 In this chapter, we're going to talk about mapping URLs
0:02 to our action or view methods.
0:04 And this is super important to think about
0:07 how we structure the URLs and exchange data
0:10 throughout our application.
0:13 When you think of the URLs
0:14 they're often not just things that are behind the scenes
0:19 in a hypertext and you click on them and whatnot in HTML
0:22 but often users perceive the URLs
0:25 as almost like a command line interface to your application.
0:28 They look up, they see the URL
0:30 they try to maybe play with it
0:32 if they can't find their way around, stuff like that, right?
0:34 So you want to have a super clean set of URLs
0:37 both for you 'cause it helps you organize your code
0:40 and group it, and so on, but also so that users
0:43 have the cleanest possible interaction.
0:46 You might also think about SEO
0:47 or search engine optimization.
0:49 One of the big triggers or cues to Google
0:52 on what this page is about is what is in that URL.
0:56 URLs are super important
0:58 and luckily, Flask makes working with them pretty easy.
1:01 Now, let's visualize how this routing
1:04 or routing if you prefer your British English, works.
1:07 So the idea is we have our web browser on the left
1:10 that some client that wants to get to our website.
1:12 We have this kind of funky visualization of our server
1:15 and our views and their various methods
1:17 that we're going to map URLs over to.
1:20 So we have our package views and our home views.
1:22 We've broken those into two distinct areas
1:24 to do with PyPI packages and views.
1:27 We're going to get to that real soon.
1:29 And then each of those have a couple of methods
1:31 that could be called here.
1:33 So request can come in
1:35 and HTTP GET request is going to go to project/sqlalchemy
1:40 and with a query string,?mode=edit.
1:45 So it's up to the routing to figure out
1:47 do I have any of these view methods on the right that match
1:50 and which one should it be?
1:53 So we've registered some routes with the app.route decorator.
1:56 So for /, we said that goes
1:58 to home_views.index, that function.
2:01 So Flask is going to ask is this a match?
2:04 And in this case, it has to be an exact string match
2:07 up to, but not including, the query string.
2:09 So it's /project/sqlalchemy, no
2:13 so I'm going to skip over that.
2:14 Next, I'm going to have is /about.
2:16 Also, because there's no variable component here
2:18 this has to match the root part of the request.
2:23 Nope. /project is not /about.
2:26 The next one we're going to define is /project/package
2:29 and notice the angle brackets there.
2:31 The angle brackets mean this part
2:33 is some variable component.
2:36 So in our case above, we have /project/sqlalchemy
2:39 and here we have /project/ something.
2:41 And we're giving it a name
2:43 so that we can actually work with that value.
2:45 This one turns out is a match
2:48 so it's going to go and prepare the request
2:50 and then call package_views.details.
2:52 And Flask is really nice because what Flask will do
2:55 it will take all of the arguments or values
2:57 that are in the route, in the URL here, so package for now
3:01 and it will pass that as a parameter into our function.
3:04 And we can even define some of them as strings
3:06 and some of them as integers, and things like that.
3:08 So really, really nice.
3:09 It will take those variables that are in the URL
3:12 and pass them through.
3:13 It will also gather up the query string
3:16 and put that onto request.args.
3:18 Remember, request is like a ambient global thing.
3:21 It doesn't get passed in the method.
3:23 You say flask.request
3:24 and it has whatever request is running.
3:26 So mode is edit. So that's it.
3:30 That's the general idea of routing.
3:32 We're going to define a set of URLs.
3:33 Sometimes they're static like the first two.
3:35 Sometimes they have variable components in them
3:38 like this last one.
3:39 And then we're going to associate those
3:41 with various view methods
3:42 and Flask will look at each request
3:44 figuring out which one to router to, and then call that one.
3:47 Or if there's none that match
3:48 it will just return a 404, Page Not Found.