Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Course conclusion
Lecture: Routes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Once we have our view methods in place
0:01
and the templates that are going to show their html
0:04
we have to figure out how our URLs
0:06
map over to those view methods
0:08
and that's where routing comes in.
0:09
So we can simply refer to the static folder
0:13
like /static/css/site.css
0:17
and we don't have to do anything for that to be mapped.
0:19
Flask will already do that for you
0:21
but maybe we want to go to one of our view methods
0:23
and say this one is going to
0:24
respond to /
0:26
and this other one is going to respond to /about.
0:29
While these pages themselves
0:31
what's actually shown in the html
0:32
may be not be totally fixed and static
0:35
the actual route will.
0:36
We're not passing additional data
0:38
along the route.
0:39
But sometimes, you do want to
0:41
like in our package, we want to show
0:43
different packages, and that's actually part of the URL.
0:46
So we could say
0:47
we want to map /project/<package_name>
0:49
and what we put in angle brackets here
0:51
will become variables in the view method
0:54
and that will be passed along
0:55
whatever value gets put in there
0:57
automatically by Flask.
0:58
We can even have constraints.
1:00
Remember we did our popular packages there?
1:03
Is that if you do a /number, like /1, /7, whatever
1:08
we want to go and actually call this popular route.
1:12
However, if for some reason, you pass /about
1:14
it's not going to match that, right?
1:15
And that's because of the int: there in that screen.
1:20
That's it. It's also a good idea to define these
1:22
from more specific to more general
1:24
so Flask is going to hit the first one that it knows about
1:28
and just call that method, so you want to do that as well.
1:30
by the order in which you register blueprints
1:33
and things like that.