1. 程式人生 > 其它 >flask鉤子函式

flask鉤子函式

請求鉤子

通過裝飾器為一個模組新增請求鉤子, 對當前模組的請求進行額外的處理. 比如許可權驗證.

應用鉤子函式

before_first_request

在對應用程式例項的第一個請求之前註冊要執行的函式, 只會執行一次

#: A lists of functions that should be called at the beginning of the
    #: first request to this instance.  To register a function here, use
    #: the :meth:`before_first_request` decorator.
    #:
    #: .. versionadded:: 
0.8 self.before_first_request_funcs = [] @setupmethod def before_first_request(self, f): """Registers a function to be run before the first request to this instance of the application. .. versionadded:: 0.8 """ self.before_first_request_funcs.append(f)

將要執行的函式存放到before_first_request_funcs 屬性中進行儲存

before_request

在每個請求之前註冊一個要執行的函式, 每一次請求都會執行

#: A dictionary with lists of functions that should be called at the
   #: beginning of the request.  The key of the dictionary is the name of
   #: the blueprint this function is active for, `None` for all requests.
   #: This can 
for example be used to open database connections or #: getting hold of the currently logged in user. To register a #: function here, use the :meth:`before_request` decorator. self.before_request_funcs = {} @setupmethod def before_request(self, f): """Registers a function to run before each request.""" self.before_request_funcs.setdefault(None, []).append(f) return f

將要執行的函式存放在字典中, None 為鍵的列表中存放的是整個應用的所有請求都要執行的函式.
after_request

在每個請求之後註冊一個要執行的函式, 每次請求都會執行. 需要接收一個 Response 類的物件作為引數 並返回一個新的Response 物件 或者 直接返回接受到的Response 物件

#: A dictionary with lists of functions that should be called after
    #: each request.  The key of the dictionary is the name of the blueprint
    #: this function is active for, `None` for all requests.  This can for
    #: example be used to open database connections or getting hold of the
    #: currently logged in user.  To register a function here, use the
    #: :meth:`after_request` decorator.
    self.after_request_funcs = {}

    @setupmethod
    def after_request(self, f):
        """Register a function to be run after each request.  Your function
        must take one parameter, a :attr:`response_class` object and return
        a new response object or the same (see :meth:`process_response`).

        As of Flask 0.7 this function might not be executed at the end of the
        request in case an unhandled exception occurred.
        """
        self.after_request_funcs.setdefault(None, []).append(f)
        return f

將要執行的函式存放在字典中, None 為鍵的列表中存放的是整個應用的所有請求都要執行的函式.

teardown_request

註冊一個函式在每個請求的末尾執行,不管是否有異常, 每次請求的最後都會執行.

#: A dictionary with lists of functions that are called after
    #: each request, even if an exception has occurred. The key of the
    #: dictionary is the name of the blueprint this function is active for,
    #: `None` for all requests. These functions are not allowed to modify
    #: the request, and their return values are ignored. If an exception
    #: occurred while processing the request, it gets passed to each
    #: teardown_request function. To register a function here, use the
    #: :meth:`teardown_request` decorator.
    #:
    #: .. versionadded:: 0.7
    self.teardown_request_funcs = {}

    @setupmethod
    def teardown_request(self, f):
        """Register a function to be run at the end of each request,
        regardless of whether there was an exception or not.  These functions
        are executed when the request context is popped, even if not an
        actual request was performed.
        """
        self.teardown_request_funcs.setdefault(None, []).append(f)
        return f

將要執行的函式存放在字典中, None 為鍵的列表中存放的是整個應用的所有請求都要執行的函式.