Hardware and software Environment

  • Windows 10 64bit
  • Anaconda3 with python 3.7
  • PyCharm 2019.3
  • Flask 1.1.1

News flash

Introduction to the

In Web applications, it is often necessary to provide feedback on the user’s actions so that the user knows exactly what is happening. The most common way, of course, is to display some character on the web page, be it a confirmation message, a warning or an error alert.

Flask implementation

In Flask, you use flash messages. In Flask, you use flash().

flash(message, category)

Among them

  • “Message” : indicates the specific message content
  • Category: An optional parameter that indicates the message type, such as error or warning

After sending a message in the view function, we naturally need to retrieve the message in the template file, using the method get_flashed_message

get_flashed_messages(with_categories, category_filter)

Both parameters are optional

  • With_categories: Message type, and aboveflashmatching
  • Category_filter: filter condition

Let’s look at a complete example

Run. Py File contents

from flask import Flask, render_template, request, redirect, url_for, flash
app = Flask(__name__)
app.secret_key = "xxx"

@app.route('/')
def index() :
    return render_template('index.html')

@app.route('/login', methods=['GET'.'POST'])
def login() :
    error = None
    if request.method == "POST":
        if request.form['email'] != '[email protected]' or request.form['password'] != 'test':
            error = "Invalid account."
        else:
            flash("Login successfully")
            return redirect(url_for('index'))
    return render_template('login.html', error=error)

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=5000,debug=True)
Copy the code

The Flash method is called when the mailbox and password are entered correctly

Template file index.html

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
    {% with messages = get_flashed_messages() %}
         {% if messages %}
               {% for message in messages %}
                    <p>{{ message }}</p>
               {% endfor %}
         {% endif %}
    {% endwith %}

<h3>Welcome!</h3>
<a href = "{{ url_for('login') }}">login</a>
</body>
</html>
Copy the code

Get all the messages by calling the get_flashed_messages method, and then display each message using a for-in loop. At the bottom of the page, we place a hyperlink to jump to the Login page

Login. HTML file contents

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <form method = "post" action = "http://localhost:5000/login">
        <table>
            <tr>
                <td>Email</td>
                <td><input type = 'email' name = 'email'></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type = 'password' name = 'password'></td>
            </tr>
            <tr>
                <td><input type = "submit" value = "Submit"></td>
            </tr>
        </table>
    </form>

    {% if error %}
        <p><strong>Error</strong>: {{ error }}</p>
    {% endif %}
</body>
</html>
Copy the code

This is the simple login screen we introduced earlier, with the error message displayed at the bottom

Finally, start the Flask service and go to http://127.0.0.1:5000

Enter email and password

Error, invalid account information displayed

If yes, the welcome message is displayed