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