Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Introducing the Flask framework
Lecture: Building block: Routes and URLs
0:00 The next core building block is route.
0:03 Now, conceptually, maybe this should go first
0:05 but I wanted to show you what the view method
0:07 looked like because routes go on them.
0:09 So here we have a route, and it has a URL pattern.
0:12 We've got /project, that part is fixed.
0:15 It's always going to be the same in the URL.
0:18 The part that comes after that, that's actually defining
0:21 what we want to pull from the database.
0:23 So like, /project/boto or /project/sqlalchemy
0:29 That last part, the package name is actually going to be
0:32 passed in as a string to our details function here.
0:36 And then we're going to do some processing with that.
0:37 Of course, you know, go to the database
0:39 maybe tell them there's a 404 if they try to
0:42 get a package that literally doesn't exist.
0:44 Otherwise we're just going to show them the details.
0:46 So for routes, we use app.route as a decorator
0:50 with a unique URL pattern, an optional HTTP verb.
0:54 Here we don't see one but previously
0:56 in that register example, we said it only accepts
0:58 post not get requests so only the form submission
1:01 not the original request.
1:03 And then some route data like URL packaging.
1:06 If we want to serve up static files, we don't have to
1:08 define a route for that, just /static will automatically
1:11 be used by Flask but of course
1:13 you can go customize that as well.