Flask中endpoint的理解(小結(jié))
在flask框架中,我們經(jīng)常會遇到endpoint這個東西,最開始也沒法理解這個到底是做什么的。最近正好在研究Flask的源碼,也就順帶了解了一下這個endpoint
首先,我們看一個例子:
@app.route('/user/<name>') def user(name): return 'Hello, %s' % name
這個是我們在用flask框架寫網(wǎng)站中最常用的。
通過看源碼,我們可以發(fā)現(xiàn):
函數(shù)等效于
def user(name) return 'Hello, %s' % name app.add_url_rule('/user/<name>', 'user', user)
這個add_url_rule函數(shù)在文檔中是這樣解釋的:
add_url_rule(*args, **kwargs)
Connects a URL rule. Works exactly like the route() decorator. If a view_func is provided it will be registered with the endpoint.
add_url_rule有如下參數(shù):
rule – the URL rule as string
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
view_func – the function to call when serving a request to the provided endpoint
options – the options to be forwarded to the underlying Rule object. A change to Werkzeug is handling of method options. methods is a list of methods this rule should be limited to (GET, POST etc.). By default a rule just listens for GET (and implicitly HEAD). Starting with Flask 0.6, OPTIONS is implicitly added and handled by the standard request handling.
拋開options這個參數(shù)不談,我們看看前三個參數(shù)。
rule:這個參數(shù)很簡單,就是匹配的路由地址
view_func:這個參數(shù)就是我們寫的視圖函數(shù)
endpoint:這個參數(shù)就是我今天重點要講的,endpoint
很多人認為:假設用戶訪問http://www.example.com/user/eric,flask會找到該函數(shù),并傳遞name='eric',執(zhí)行這個函數(shù)并返回值。
但是實際中,F(xiàn)lask真的是直接根據(jù)路由查詢視圖函數(shù)么?
在源碼中我們可以發(fā)現(xiàn):
- 每個應用程序app都有一個view_functions,這是一個字典,存儲endpoint-view_func鍵值對。add_url_rule的第一個作用就是向view_functions中添加鍵值對(這件事在應用程序run之前就做好了)
- 每個應用程序app都有一個url_map,它是一個Map類(具體實現(xiàn)在werkzeug/routing.py中),里面包含了一個列表,列表元素是Role的實例(werkzeug/routing.py中)。add_url_rule的第二個作用就是向url_map中添加Role的實例(它也是在應用程序run之前就做好了)
我們可以通過一個例子來看:
app = Flask(__name__) @app.route('/test', endpoint='Test') def test(): pass @app.route('/', endpoint='index') def hello_world(): return 'Hello World!' if __name__ == '__main__': print(app.view_functions) print(app.url_map) app.run()
運行這個程序,結(jié)果是:
{'static': <bound method Flask.send_static_file of <Flask 'flask-code'>>, 'Test': <function test at 0x10065e488>, 'index': <function hello_world at 0x10323d488>}
Map([<Rule '/test' (HEAD, OPTIONS, GET) -> Test>,
<Rule '/' (HEAD, OPTIONS, GET) -> index>,
<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
所以我們可以看出:這個url_map存儲的是url與endpoint的映射!
回到flask接受用戶請求地址并查詢函數(shù)的問題。實際上,當請求傳來一個url的時候,會先通過rule找到endpoint(url_map),然后再根據(jù)endpoint再找到對應的view_func(view_functions)。通常,endpoint的名字都和視圖函數(shù)名一樣。
這時候,這個endpoint也就好理解了:
實際上這個endpoint就是一個Identifier,每個視圖函數(shù)都有一個endpoint,
當有請求來到的時候,用它來知道到底使用哪一個視圖函數(shù)
在實際應用中,當我們需要在一個視圖中跳轉(zhuǎn)到另一個視圖中的時候,我們經(jīng)常會使用url_for(endpoint)去查詢視圖,而不是把地址硬編碼到函數(shù)中。
這個時候,我們就不能使用視圖函數(shù)名當endpoint去查詢了
我們舉個例子來說明。比如:
app = Flask(__name__) app.register_blueprint(user, url_prefix='user') app.register_blueprint(file, url_prefix='file')
我們注冊了2個藍圖。
在user中(省略初始化過程):
@user.route('/article') def article(): pass
在file中(省略初始化過程):
@file.route('/article') def article(): pass
這時候,我們發(fā)現(xiàn),/article這個路由對應了兩個函數(shù)名一樣的函數(shù),分別在兩個藍圖中。當我們使用url_for(article)調(diào)用的時候(注意,url_for是通過endpoint查詢url地址,然后找視圖函數(shù)),flask無法知道到底使用哪個藍圖下的endpoint,所以我們需要這樣:
url_for('user.article')
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
如何利用python實現(xiàn)圖片轉(zhuǎn)化字符畫
這篇文章主要介紹了如何利用python實現(xiàn)圖片轉(zhuǎn)化字符畫,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06提升Python項目整潔度使用import?linter實例探究
在復雜的Python項目中,良好的代碼組織結(jié)構是維護性和可讀性的關鍵,本文將深入研究?import-linter?工具,它是一個強大的靜態(tài)分析工具,旨在優(yōu)化項目的模塊導入,提高代碼質(zhì)量和可維護性2024-01-01Python tkinter實現(xiàn)的圖片移動碰撞動畫效果【附源碼下載】
這篇文章主要介紹了Python tkinter實現(xiàn)的圖片移動碰撞動畫效果,涉及Python tkinter模塊操作圖片基于坐標動態(tài)變換與數(shù)值判定實現(xiàn)移動、碰撞檢測等相關操作技巧,需要的朋友可以參考下2018-01-01TensorFlow人工智能學習數(shù)據(jù)填充復制實現(xiàn)示例
這篇文章主要為大家介紹了TensorFlow人工智能學習如何進行數(shù)據(jù)填充復制的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11