Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Adding a constrained route

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's go and add the rest of the routes to our application here. So we've got our home_views with index and about. I got packages.
0:08 Let's go and create another one of these. So we have our blueprint for project details. But what I want to do is just get the popular ones.
0:17 Now I want to do something really cool with this so let's just have this return. Details for most popular package. It's not going to be exactly perfect
0:30 but now I'm going to pass in a rank and it's going to be an integer like so. How do we do that in the URL? So what I want to do actually is
0:40 I'd like to be able to come over here. Is this going to work? let me just make a quick change here. Now it should run.
0:50 What I want to do is something pretty awesome. So if we go over here and we say /about we get this. If I say /project/something
0:58 I get something else. But I want to say if the thing happens to be a number like 7 or 5 or 4 or whatever I want to run just this function.
1:10 Now if it was abc I still want it to do 'not found'. But if it's just the number right like show me the fifth most popular one
1:19 I would like to pull up that package details. I'll give you an example of that as well. So if we go over to talkpaython.fm
1:24 website podcast and you go to the episode you can see they have great long URL names like /episodes/show/206
1:33 and then a shortened sort of friendly version of the title. But sometimes it's nice sometimes you just want to share really quickly
1:41 oh like hey that was show 206 like this. If you click that and check that out it goes right over. Want to know what show 205 was?
1:51 There you go, that's what it is. So there's this cool way to capture only integers. There's other stuff that shows up there.
1:57 Like if I go to just /episodes that matches but I want to say if it's an integer we're going to pull it from the database. So let's see that over here.
2:06 And there's a pretty slick way to do this. I'm going to go and just say we're going to have a /<int:rank> and this is going to be a variable.
2:13 So we want to say this but only in the case that it's an integer. So there's a way to do that in Flask. You just say int:rank.
2:22 And now if we go and run this and we go I want to go to about we get about. But if I want to go to /7 details for the seventh most popular package.
2:32 /5, fifth most popular package. But we can still go to all the rest of our site is still working just fine. Isn't that cool?
2:41 So anytime you want to have some sort of constraint like this we can put the type in front and also let's just do a real quick print
2:49 type of rank and the value of rank real quick there. And let's look again at 11. See that Flask already converts that to an integer.
3:01 So it's not like working in string but it could be an int. No it's actually converted to an integer for us which is pretty cool. And if it goes abc
3:10 it doesn't match that route so it just says 404. If we didn't have that integer constraint you know potentially that would hit
3:16 and cause a problem right. So if we don't have that here for example now I can see details for the abc-th. That doesn't even make any sense right.
3:26 So we can say only match this short sort of Flask / some value if it happens to be an integer. Really cool we're going to add
3:34 the popular route that way. Now the details are not implemented yet we'll get to that. But a pretty cool way to add this specific route here.


Talk Python's Mastodon Michael Kennedy's Mastodon