[Flask] Flask message flashes

Flask message flashes

A good GUI-based application will provide users with feedback about the interaction. For example, desktop applications use dialog boxes or message boxes, and JavaScript uses alerts for similar purposes.

It is easy to generate such informational messages in Flask web applications. The flash system of the Flask framework can create a message in a view and present it in a view function named next .

The Flask module contains the flash() method. It passes the message to the next request, which is usually a template.

flash(message, category)

among them,

  • The message parameter is the actual message to be flashed.

  • The category parameter is optional. It can be "error", "info" or "warning".

To remove messages from the conversation, the template calls get_flashed_messages() .

get_flashed_messages(with_categories, category_filter)

Both parameters are optional. If the received message has a category, the first parameter is a tuple. The second parameter is only used to display specific messages.

index2.html

<!DOCTYPE html>
<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('login2') }}">login</a>
</body>
</html>

login2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <form method = "post" action = "http://localhost:5000/login2">
        <table>
            <tr>
                <td>Username</td>
                <td><input type = 'text' name = 'username'></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>

The above html files are in the templates folder

flash.py

from flask import Flask, flash, redirect, render_template, request, url_for
app = Flask(__name__)
app.secret_key = 'random string'
@app.route('/')
def index2():
    return render_template('index2.html')

@app.route('/login2', methods = ['GET', 'POST'])
def login2():
    error = None
    if request.method == 'POST':
        if request.form['username'] != 'admin' or \
            request.form['password'] != 'admin':
            error = 'Invalid username or password. Please try again!'
        else:
            flash('You were successfully logged in')
            return redirect(url_for('index2'))
    return render_template('login2.html', error = error) #这里其实相当于重新加载了,所以才会判断对错


if __name__ == "__main__":
    app.run(debug = True)

 

Guess you like

Origin blog.csdn.net/u013066730/article/details/108363332