Flask Tutorial – Creating Flask Flash Messages

In this Flask Tutorial we are going to to learn Creating Flask Flash Messages, so good

applications and user interfaces are all about feedback. If the user does not get enough

feedback they will probably end up hating the application. Flask provides a really simple

way to give feedback to a user with the flashing system. The flashing system basically makes

it possible to record a message at the end of a request and access it next request and only next

request. This is usually combined with a layout template that does this. Note that browsers

and sometimes web servers enforce a limit on cookie sizes. This means that flashing messages

that are too large for session cookies causes message flashing to fail silently.

 

 

 

OK we are going to use our Flask Project from the previous article, where we have a login

form, so now i want when a user enter wrong password or username, i want to show error

in Flask Flash Messages.

 

 

So this is our forms.py file, and in this file we have created our simple login form and you

can see that we have added some ready validators from Flask-WTF that is InputRequired(). 

For more information about Flask-WTF you can read this article Flask Forms with Flask-WTF.

 

 

 

 

And this is our app.py file with our required view functions and you can see that we have also

added our login view function in here. so in the Login view function we need to write our Flask

Flash Messages, till now we haven’t learned about database functionality in Flask, we are going

to just manually check the username and password. make sure that you have imported the flash.

 

 

 

 

So now this is the complete code for app.py file.

 

 

 

 

This is our base.html, we have already talked about creating templates in Flask, you can

read this article Introduction to Flask Templates. you need to just create templates folder

in your working directory. 

 

 

 

 

we are going to show our Flask Flash Messages in login.html, so you can use this code for

showing the Flash Messages. right now there is no design for the Flash Message, but we will

do that.

 

 

 

 

 

OK now this is the complete code for our login.html.

 

templates/login.html 

 

 

 

 

And this is our index.html file.

 

 

templates/index.html

 

 

 

 

Now run the project and go to http://localhost:5000/login, if you enter a wrong username

and password, you will see a Flash Message.

Flask Flash Messages
Flask Flash Messages

 

 

 

Adding Bootstrap Styles to Flash Message 

So our code is working, but the Flash Message is ugly, we need to add some bootstrap design

for the Flash Message. so now this is the new code for Flash Message. we have just added a 

simple design.

 

 

 

Now This is the complete code for login.html.

 

templates/login.html

 

 

 

 

 

Now run the project and go to http://localhost:5000/login, if you enter a wrong username

and password, you will see a nice Flash Message.

Flask Tutorial - Creating Flask Flash Messages
Flask Tutorial – Creating Flask Flash Messages

 

 

 

 

 

 

Also you can watch my complete 4 hours training on Flask Web Development

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×