message
- Settings: Flash ( ' AAA ' ) - values: get_flashed_message () - assuming a page operation error, jump to the page b error message is displayed in a page page b
1 If you use the flash must be set = app.secret_key 'asdfasdf' 2 can only take once, there is no taking 3 we can use the flash ( ' general information ' , category = " info " ), do classified information by category 4get_flashed_messages (with_categories = True, = category_filter ( " error " ,)), is provided in the form of key-value pairs with_categories acquisition of setting category_filter = ( "error",) filtered classification information
Sample code
fromFlaskImportthe Flask, Flash, get_flashed_messages App= the Flask (the __name__) app.secret_key = 'asdfasdf' @ app.route ('/ index1,') DEFindex (): #(category = "Message", Message)) Flash ('Time-out error', category ="error") # setting information, classified as error Flash ('general information', category ="info") # setting information, classified as info return "ssdsdsdfsd" @app.route('/error') def error(): data = get_flashed_messages(with_categories=True,category_filter=("error","info")) data1 = get_flashed_messages(with_categories=True, category_filter=("error", "info")) print("data1",data1) print("data",data) return "错误信息" if __name__ == '__main__': app.run()
Operating procedures
Index1 routing request, perform flash, setting information, and the classification
request routing error, execute get_flash_message. with_categories parameters can write from time to write, to write a parameter is the (type, setting information) format. category_filter which is set to display classified information
Set with_categories = True, Print Format
Do not set with_categories = True, Print Format
Request extension
1.before_request do something before accessing the official view of the function (for example, you can do it based on user login)
Note: If you join before the return in a return value, it will terminate the code is run, it will not go down.
from the Flask Import the Flask, Request, render_template App = the Flask ( __name__ ) @ app.before_request DEF befor1 (): Print (Request) Print ( " Before I request 1 " ) @ app.before_request DEF befor2 (): Print ( " I before requesting a 2 " ) @ app.route ( ' / index ' ) DEF index (): Print ( " I really view " ) return render_template ( "index.html") if __name__ == '__main__': app.run()
The result is run from the top down
2.after_request (a binding function after each request, similar to django middleware process_response)
from the Flask Import the Flask, Request, render_template App = the Flask ( __name__ ) @ app.after_request DEF after1 (the Response): Print ( " I request after 1 " ) return the Response @ app.after_request DEF after2 (the Response): Print ( " I after the request is 2 " ) return the Response @ app.route ( ' / index ' ) DEF index (): Print ( " I really view " ) return render_template("index.html") if __name__ == '__main__': app.run()
The result is run from the bottom up
3.before_first_request (run before the request, if you do not restart, run only the first time)
from flask import Flask,render_template app = Flask(__name__) @app.before_first_request def before_first(): print("123") @app.route('/index') def index(): print("我是真的视图") return render_template("index.html") if __name__ == '__main__': # app.__call__ app.run()
4.teardown_request (binding after a formal request for a function, whether or not an exception will be executed, if this is not abnormal parameters e returns None, there is an exception to this record)
from Flask Import the Flask, the render_template App = the Flask ( the __name__ ) @ app.teardown_request DEF Tear (E): Print ( ' teardown_request ' ) Print (E) # return exception information @ app.route ( ' / index ' ) DEF index () : Print ( " I really view " ) return render_template ( " index.html " ) IF __name__ == ' __main__ ': # app.__call__ app.run()
5.errorhandler (when the path does not exist 404, 500 Internal Server Error)
app.errorhandler @ (404 ) DEF error_404 (Arg): Print (Arg) # return path error return " 404 mistakes " @ app.errorhandler ( 500 ) DEF error (Arg): Print (Arg) # server returns an error message return " 500 mistakes "
6.template_global (label)
app.template_global @ () DEF SB (a1, a2): return a1 + a2 @ app.route ( ' / index ' ) DEF index (): Print ( " I really view " ) return render_template ( " index.html " )
html
{{sb(1,2)}}
7.template_filter (filter)
@app.template_filter() def db(a1, a2, a3): return a1 + a2 + a3 html #{{ 1|db(2,3)}}
to sum up:
1 master key before_request and after_request,
2 Note that there are a plurality, the order of execution
3 before_request after (that is, there is return value) intercepts the request, response all perform
Middleware (understand)
from Flask Import the Flask App = the Flask ( the __name__ ) @ app.route ( ' / ' ) DEF index (): return ' the Hello World! ' # analog intermediate class Md (Object): DEF the __init__ (Self, old_wsgi_app): Self. old_wsgi_app = old_wsgi_app DEF the __call__ (Self, Environ, The start_response): Print ( ' before starting ' ) RET =self.old_wsgi_app (Environ, start_response) Print ( ' after the ' ) return RET IF __name__ == ' __main__ ' : # 1 we find that when executed app.run method, the final execution run_simple, final implementation app (), that is, in the execution app .__ call__ method # 2 in __call__ inside, execution is self.wsgi_app (). we hope that before his own execution wsgi do something. # 3 So we will start with Md class __init__, wsgi before saving, then we will app.wsgi transformed into an object of Md. # 4 that the implementation of the new app.wsgi_app, is the implementation of __call__ Md method. # The original wsgi_app replace custom, app.wsgi_app = Md (app.wsgi_app) app.run ()
All processes requests
= CTX self.request_context (Environ) error = None the try : the try : ctx.push () # to perform a function of view, the view class the path Response = self.full_dispatch_request () the except Exception AS E: error = E Response = self.handle_exception (E) the except : # noqa: B001 error = the sys.exc_info () [. 1 ] The raise return Response (Environ, The start_response) a finally : # regardless of the no abnormalities will go here IF self.should_ignore_error (error): error = None ctx.auto_pop (error)