Flask Middleware

First, the request for extension

# 1 before_request 
    analogy django middleware process_request, prior to receipt of the request to bind a function to do something 

# 2 after_request 
    analogy django middleware process_response, after each request for a binding function, if the request is not abnormal 

# summary 
    Key master before_request and after_request, 

    note that there are more than one, the order of execution 

    after before_request request interception (that is, there is return value), response all perform

 

 

from Flask Import the Flask, Request 

App = the Flask ( the __name__ ) 

@ app.before_request 
DEF Process (): 
    request.name = ' pdun ' 
    Print ( ' request time ' ) 

@ app.route ( ' / ' )
 DEF index () :
     Print ( ' index ---- ' , request.name)     # discharge data can be taken out 
    Print ( ' executor ' )
     return  ' index '


app.after_request @ 
DEF text (the Response):        # required parameters 
    Print ( ' text ---- ' , request.name)
     Print ( ' request to take the time ' )
     return the Response       # need to go back out 


IF  __name__ == ' __main__ ' : 
    app.run ()
Simple to use

 

# Execution order django middleware and similar to the time from the top down, from the bottom up when walking 


from Flask Import the Flask, Request 

App = the Flask ( the __name__ ) 

@ app.before_request 
DEF Process (): 
    request.name = ' pdun ' 
    Print ( ' when the request 111 ' ) 

@ app.before_request 
DEF Process (): 
    request.name = ' pdun ' 
    Print ( ' when the request 222 ' ) 


@ app.route ( ' / ' )
 DEF index ( ):
    Print ( ' executor ' )
     return  ' index ' 


@ app.after_request 
DEF text (Response):
     Print ( ' request to take the time 111 ' )
     return Response 

@ app.after_request 
DEF text (Response):
     Print ( ' request when walking 222 ' )
     return Response 


IF  the __name__ == ' __main__ ' : 
    app.run () 



when the request 111 
when a request 222 to the 
execution of the program  
when the request to go 222
when the request to go 111



 --------------------------------------------- ----------------- 

time if the request is returned, the order of execution of the request, with different Django 
from Flask Import the Flask, request 

App = the Flask ( the __name__ ) 

@ app.before_request 
DEF Process ():
     Print ( ' when the request 111 ' )
     return  ' XXXXXXXXXXX '                 # to return a time, go directly down request code 

@ app.before_request 
DEF Process ():
     Print ( ' time request 222 ' ) 


@ app.route ( ' / ' )
DEF index ():
     Print ( ' executor ' )
     return  ' index ' 


@ app.after_request 
DEF text (Response):
     Print ( ' when a request to go 111 ' )
     return Response 

@ app.after_request 
DEF text (Response):
     Print ( ' requests take the time 222 ' )
     return Response 


IF  the __name__ == ' __main__ ' : 
    app.run () 




when the request 111 
requests take the time 222
When requested to go 111
Execution order, the request is returned to the execution sequence

 

# 3 before_first_request 
    service started, the first request will go, then do not go after the 

# 4 teardown_request 
    regardless of whether an exception occurs, will be executed 

# 5 ErrorHandler 
    @ app.errorhandler (404)        # can you write state 
    DEF error_404 (Arg) :
         return  " 404 wrong " 

# . 6 template_global label 
    @ app.template_global ()
     DEF the Add (A1, A2):
         return A1 + A2
     # {{the Add (1,2)}} # view function (execution program) do not need to pass return add, can be used directly template layer 

# . 7 template_filter filter 
    @ app.template_filter ()
     DEF DB (A1, A2, A3):
        return A1 + A2 + A3
     # {{. 1 | DB (2,3)}} # django different and may pass a plurality of parameters (django supports only two pass)
    

 

Guess you like

Origin www.cnblogs.com/pdun/p/11204883.html