Flash
Flash() provides a very useful flash() function, which can be used to "flash" messages that need to be prompted to the user, such as "welcome back!" when the user logs in successfully. In the view function, call the flash() function to pass in the message content. The flash() function stores the message in the session. We need to use the global function get flash messages() in the template to get the message and display it.
Messages sent through the flash() function are stored in the session object, so we need to set the secret key for the program. You can set it through the app.secret'key property or the configuration variable secret'key.
Source code
def flash(message, category="message"): # The default category is message flashes = session.get("_flashes", []) flashes.append((category, message)) session["_flashes"] = flashes message_flashed.send( current_app._get_current_object(), message=message, category=category ) def get_flashed_messages(with_categories=False, category_filter=()): flashes = _request_ctx_stack.top.flashes if flashes is None: _request_ctx_stack.top.flashes = flashes = ( session.pop("_flashes") if "_flashes" in session else [] ) if category_filter: flashes = list(filter(lambda f: f[0] in category_filter, flashes)) if not with_categories: return [x[1] for x in flashes] return flashes
- Set flash
-
flash("value to be passed", category = "name of classification"), if not, the default is message
- Nature: session ['[flash']
-
flash("value to be passed", category = "name of classification"), if not, the default is message
- Take the value of flash setting, and we use get flash messages
-
get_flashed_messages(with_categories=False, category_filter=())
- If no category filter is passed, take out the values passed by all categories stored above
- If you don't pass with categories, only the value will be obtained. If you pass the value, the classification name and classification value will be obtained
-
get_flashed_messages(with_categories=False, category_filter=())
- This flash can only be taken from one view function. As long as one view function has been taken, other view functions cannot be taken
- Essence: session. Pop ("﹐ flash")
- But in the same view function, there are infinite values
- Essence: session. Pop ("﹐ flash")
case
from flask import Flask,flash,get_flashed_messages,request,redirect app = Flask(__name__) app.debug=True app.secret_key = 'asdfasdf' @app.route('/index') def index(): # Get all the set values from somewhere and clear them. #flash('timeout error ', category="x1") flash("It's coming. You have to be careful") flash("I'm the second",category="ss") # return "ssdsdsdfsd" return redirect('/error') @app.route('/error') def error(): """ //Display error messages :return: //If get ﹐ flashed ﹐ messages (with ﹐ category = true) """ #data = get_flashed_messages(category_filter=['x1']) data=get_flashed_messages(with_categories=True,category_filter=['ss']) data1 = get_flashed_messages(with_categories=True, category_filter=['ss']) print(type(data)) print(data1) return "Error message:%s" %(data,) if __name__ == '__main__': app.run()
middleware
The essence is to encapsulate the original execution function
Note: the request extension of Flask is the middleware of Django. Django's middleware is not Flask's Middleware
Custom local Middleware
According to the decorator
from functools import wraps def auth(func): @wraps(func) def inner(*args, **kwargs): if not session.get('user'): return redirect(url_for('login')) ret = func(*args, **kwargs) return ret return inner @app.route('/login', methods=["GET", "POST"]) @auth def login(): return "Sign in"
Custom global decorator
Write in app.py
class Middleware(object): def __init__(self, old): self.old = old def __call__(self, *args, **kwargs): print('Action before request') ret = self.old(*args, **kwargs) print('Action after request') return ret if __name__ == '__main__': app.wsgi_app = Middleware(app.wsgi_app) app.run()