Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Routing and URLs
Lecture: Concept: Create route from url
0:00 Let's really quickly review how given a URL
0:03 we're going to define a view method
0:05 and a route in Flask to match that.
0:07 So suppose we're going to go to /help/topic
0:10 maybe there's some kind of Help System
0:12 /help/faqs, /help/gettingstarted, I don't know
0:16 and those are going to return different bits of detail.
0:19 So how do we create something in Flask to handle that
0:22 and pass topic to that function?
0:25 So we're going to add an app.route decorator
0:28 as you do for all the view methods.
0:29 I'm going to call this Help
0:30 'cause that seems reasonable to me
0:32 but you can call the function whatever you want
0:33 as long as it's distinct.
0:35 Then it's going to be /help/, that's the static part
0:39 it's always like that, no variables are passed there.
0:42 No data is being passed there.
0:44 But then, the part that goes on the end /summary
0:46 /gettingstarted, /faq
0:49 we want to pass that into the function
0:50 so we put in <topic_name>
0:55 So that defines a variable
0:56 that gets passed into our function, topic_name
0:59 and I like to give them type annotations
1:01 'cause sometimes, as we say you can make them integers
1:03 and what not.
1:04 So we're going to pass that in
1:05 and then you just do your work, right?
1:07 Go to the database, call the service
1:09 whatever you got to do to render, to display that content.
1:13 Super nice and easy to work with routing system
1:16 you don't have to work with regular expressions
1:18 or other crazy stuff that some other frameworks have you do.
1:21 No you just put stuff in angle brackets
1:23 and that gets passed along as parameters to your methods.
1:26 Couldn't be easier.