Flask Flashing


Python Flash

  • flash () - It is used to generate informative messages in the flask. It creates a message in one view and renders it to a template view function called next.
  • flash() method of the flask module passes the message to the next request which is an HTML template.


 Flash Syntax

Flash Syntax

flash(message, category)  
  • Category - It is an optional parameter which may represent any error, information, or warning ?
  • Message - It is the message to be flashed to the user.

The messages are generated in the flask script using the flash() method of flask module. These messages need to be extracted in the template from the session. For this case, the method get_flashed_messages() is called in the HTML template.




get_flashed_messages (with_categories, category_filter)
  • with_categories: This parameter is optional and used if the messages have the category.
  • category_filter: This parameter is also optional. It is useful to display only the specified messages.

Sample code

The following code contains the flask and HTML scripts for server and client-side scripting.


from flask import *  
app = Flask(__name__)  
app.secret_key = "abc"  
def home():  
    return render_template("index.html")  
@app.route('/login',methods = ["GET","POST"])  
def login():  
    error = None;  
    if request.method == "POST":  
        if request.form['pass'] != 'jtp':  
            error = "invalid password"  
            flash("you are successfuly logged in")  
            return redirect(url_for('home'))  
    return render_template('login.html',error=error)  
if __name__ == '__main__':  
    app.run(debug = True)  


    {% with messages = get_flashed_messages() %}  
         {% if messages %}  
               {% for message in messages %}  
                    <p>{{ message }}</p>  
               {% endfor %}  
         {% endif %}  
      {% endwith %}  
<h3>Welcome to the website</h3>  
<a href = "{{ url_for('login') }}">login</a>  


    {% if error %}  
        <p><strong>Error</strong>: {{error}}</p>  
    {% endif %}  
    <form method = "post" action = "/login">  
            <tr><td>Email</td><td><input type = 'email' name = 'email'></td>
            <tr><td>Password</td><td><input type = 'password' name = 'pass'>
            <tr><td><input type = "submit" value = "Submit"></td></tr>  


  • The URL /index displays the following template (index.html) which contains the code for flashing the message. The link login redirects the user to the URL /login.

Flash Home

  • The following page shows the template login.html. The user to enter the valid email id and password. Here, if the user enters any wrong password other than "123", it can not be able to login to the application.

Flash Login

  • The script login.py generates an error message as "invalid password" and redirects the user to this page itself.

Flash invalid

  • The second case where the user has entered the correct password as 123 and the script flashing.py flashes the success message.

Flash Logged

If you want to learn about Python Course , you can refer the following links Python Training in Chennai , Machine Learning Training in Chennai , Data Science Training in Chennai , Artificial Intelligence Training in Chennai

Related Searches to Flask Flashing

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.