From the course: Flask Essential Training
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
View functions
From the course: Flask Essential Training
View functions
- [Instructor] View functions are Python functions that are called when a specific route is accessed. And these functions handle the logic of what happens when a user requests a particular URL, which is essential for delivering dynamic content and interactions in your app. Let's write a view function to display a post. We have a home route defined. I have a little posts dictionary. We type the decorator, and this is going to be post/1. And here we define a function. show_post. And inside, we save the first post in the post variable. And next we return some HTML. Open a paragraph tag, and here we display the content of the post, and we close the tag. Let's give ourselves a little bit of space. All right, so the App route post, for the first post, this line uses the route decorator to tell Flask that the function show_post should handle requests to the URL path for the first post. It effectively maps the URL /post/1 to this function. This line defines the view function itself. So…