国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Flask之請求鉤子的實(shí)現(xiàn)

2020-02-16 00:16:12
字體:
供稿:網(wǎng)友

請求鉤子

通過裝飾器為一個(gè)模塊添加請求鉤子, 對當(dāng)前模塊的請求進(jìn)行額外的處理. 比如權(quán)限驗(yàn)證.

說白了,就是在執(zhí)行視圖函數(shù)前后你可以進(jìn)行一些處理,F(xiàn)lask使用裝飾器為我們提供了注冊通用函數(shù)的功能。

1、before_first_request:在處理第一個(gè)請求前執(zhí)行

before_first_request

在對應(yīng)用程序?qū)嵗牡谝粋€(gè)請求之前注冊要運(yùn)行的函數(shù), 只會(huì)執(zhí)行一次

  #: 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) 

將要運(yùn)行的函數(shù)存放到before_first_request_funcs 屬性中進(jìn)行保存

2、before_request:在每次請求前執(zhí)行

在每個(gè)請求之前注冊一個(gè)要運(yùn)行的函數(shù), 每一次請求都會(huì)執(zhí)行

  #: 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

將要運(yùn)行的函數(shù)存放在字典中, None 為鍵的列表中存放的是整個(gè)應(yīng)用的所有請求都要運(yùn)行的函數(shù).

3、after_request:每次請求之后調(diào)用,前提是沒有未處理的異常拋出

在每個(gè)請求之后注冊一個(gè)要運(yùn)行的函數(shù), 每次請求都會(huì)執(zhí)行. 需要接收一個(gè) Response 類的對象作為參數(shù) 并返回一個(gè)新的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            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浦城县| 博爱县| 北票市| 茂名市| 博客| 沂源县| 平度市| 鹤壁市| 西城区| 抚远县| 论坛| 溧阳市| 武城县| 沈丘县| 琼结县| 郯城县| 嘉峪关市| 永定县| 东兴市| 乡城县| 潞城市| 长顺县| 紫金县| 靖宇县| 浦城县| 章丘市| 江孜县| 中方县| 南丰县| 湖南省| 武安市| 罗甸县| 福建省| 隆林| 梧州市| 甘谷县| 墨江| 景洪市| 卢氏县| 奉节县| 岱山县|