Flask Flashing




 Flash

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.

Syntax

 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.

Syntax

 Flashed

Flashed

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.

flashing.py

from flask import *  
app = Flask(__name__)  
app.secret_key = "abc"  
 
@app.route('/index')  
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"  
        else:  
            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)  

index.html

<html>  
<head>  
<title>home</title>  
</head>  
<body>  
    {% 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>  
</body>  
</html>  

login.html

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

Output

  • 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.
 Home

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.
 Login

Flash Login

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

Flash invalid

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

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