Flask provides the flash() method, in the same way, the client-side scripting language like JavaScript uses the alerts or the python GUI framework Tkinter uses the dialogue box or the message box.
The flash() method 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.
syntax to use the flash() method is given below.
flash(message, category)
#app.py from flask import Flask, render_template, request, flash, redirect, url_for # Initialize the Flask application app = Flask(__name__) app.secret_key = "cairocoders-ednalan23" @app.route('/') 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'] != 'ednalan': 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>Flask 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' autocomplete="off"></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>