In this video, we will review how to send messages from the server-side to templates (flash messages) and how to define and map custom error pages.
- [Instructor] Hi, and welcome to the last video of this section, Flask Flash Messages and Custom Error Pages. In the previous video, we learned how to use templates for creating webpages inside Flask. In this video, we are going to talk about flash messages, which are sent from server side to the template context. And we will see how to create and map custom error pages. First, let's check how we send flash messages to the template context. Flash messages were designed to provide feedback to the user. On this page, the two blue alerts are flash messages, and are generated by the template context.
Let's take a look at the source code that generates these. Now we open up the about.html file, and we can see that this extends the layout of the HTML page. This helps us to have the menu and the footer region appear correctly on the page. The flash messages can be accessed using the get_flashed_messages method. Flash messages can have categories. The category filter name parameter helps to filter. Using this parameter only the messages pushed with the given category are returned. In the routes.py file, we can see how the two messages are pushed to the template context.
The flash method from the Flask name space takes two parameters. The first parameter is the message and the second is the category which the message should be assigned to. Now, let's check how to use custom error pages. In the web application we have two custom error pages. First is an error page shown when the user navigates to an unmapped URL. The source code for this error page is in file 404.html. The 404 HTML extends the layout that the HTML template.
We create the new Bootstrap jumbotron and release the flash messages inside that. The second error page is displayed when an internal error occurs on the server. Each of the error pages displays the error message generated by the Flask framework inside the Bootstrap alert. The source code for this error page is in the file 500.html. The source code for this file is very similar to the error file we have taken a look at at 404.html. Let's see how to define these custom error page handlers in the application.
Let's open up routes.py and let's navigate to the init error handlers method. Inside the init error handlers method we use the error handlers spec dictionary to assign the handler methods to the different error codes. This can be seen in the init error handlers. Besides using the error handlers spec dictionary, there's an annotation called error handler which you see is on parameter, the error code which the method should be mapped to. In this video, we covered what are flash messages and how we can use these.
We also covered how to map, create and use custom error pages. In this section, we covered how to create a new development environment for Flask. After that, we check how to configure roots and handle request parameters. Then we learned how templates can be used for displaying webpages, and in the end, in this video, we covered flash messages and error pages. In the next section, we cover the Web API port in that. First, we will cover the basics of CRUD and REST. Then we will check how to SQL coming for storing models, and after that, we will implement the Create and Read API ports from CRUD.
Thank you for watching and see you in the next section.
Regardless of platform, you will need to build APIs to serve data between different client applications and endpoints. Good APIs are a necessity for web and mobile projects, especially with the modern, mobile-first approach to development. This course delivers the fundamental knowledge required to enable highly connected interactions between applications via RESTful web APIs. Follow along with Gergo Bogdan and learn how to build up and structure an effective web API that can be used by any client application accessing it over HTTP, using Flask, the Python microframework.
Find out how to implement CRUD operations using SQLAlchemy and MySQL as the data store. Get an understanding of how REST works relative to APIs, and learn how to test APIs written in Python with the support of Flask. Explore token-based authentication and find out how to store passwords securely in your database. Along the way, Gergo introduces best practices and design guidelines when building large applications.
- Setting up Flask
- Using Flask templates and routing
- Implementing CRUD operations
- Implementing pagination
- Configuring RESTful authentication
- HTTP caching
- Securing web APIs
- Testing the application