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