Flash of flask

Requirements: There are two functions login and index, one person is making a request to the login page, the login generates an error, puts it in the session, jumps to the index to display the error, and then removes the session, and this error can only be executed once (That is to let you see it once) This thing can be realized by flashing,

from flask import Flask,session,flash,get_flashed_messages
app = Flask(__name__)
app.secret_key = " sdfgergrshhsh " 
@app.route( " /x1 " ,methods=[ " GET " , " POST " ])
 def login():
     # session['msg'] = "Reply Hahahahahaha" #this Flash based on session 
    ( " Work overheating 1 " ,category= ' x1 ' )     #This is another way to set flash, which is also done internally based on session, flash actually sets this value to session 
    flash ( "The second color method is enough " ,category= ' x2 ') # category means to classify the data
    return  " view function x1 "

@app.route("/x2",methods=["GET","POST"])
def index():
    data = get_flashed_messages(category_filter=[ ' x1 ' ]) #This is to take something similar to the error message we set above. This is actually to get the value set above on the session and delete it 
    # category_filter = ['x1' ] This means to take the corresponding data of x1, if you want to take both, category_filter = ['x1','x1'] 
    print (data)
     # msg = session.pop('msg') # After this is taken, there will be no Now, this is implemented based on session. After reading it, I delete the 
    return  " view function x2 "

if __name__ == '__main__':
    app.run()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325119309&siteId=291194637