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