Flask中endpoint的理解(小結)
在flask框架中,我們經常會遇到endpoint這個東西,最開始也沒法理解這個到底是做什麼的。最近正好在研究Flask的原始碼,也就順帶了解了一下這個endpoint
首先,我們看一個例子:
@app.route('/user/<name>') def user(name): return 'Hello,%s' % name
這個是我們在用flask框架寫網站中最常用的。
通過看原始碼,我們可以發現:
函式等效於
def user(name) return 'Hello,%s' % name app.add_url_rule('/user/<name>','user',user)
這個add_url_rule函式在文件中是這樣解釋的:
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有如下引數:
rule – the URL rule as string
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpointview_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這個引數不談,我們看看前三個引數。
rule:這個引數很簡單,就是匹配的路由地址
view_func:這個引數就是我們寫的檢視函式
endpoint:這個引數就是我今天重點要講的,endpoint
很多人認為:假設使用者訪問http://www.example.com/user/eric,flask會找到該函式,並傳遞name='eric',執行這個函式並返回值。
但是實際中,Flask真的是直接根據路由查詢檢視函式麼?
在原始碼中我們可以發現:
- 每個應用程式app都有一個view_functions,這是一個字典,儲存endpoint-view_func鍵值對。add_url_rule的第一個作用就是向view_functions中新增鍵值對(這件事在應用程式run之前就做好了)
- 每個應用程式app都有一個url_map,它是一個Map類(具體實現在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()
執行這個程式,結果是:
{'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,GET) -> index>,
<Rule '/static/<filename>' (HEAD,GET) -> static>])
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
所以我們可以看出:這個url_map儲存的是url與endpoint的對映!
回到flask接受使用者請求地址並查詢函式的問題。實際上,當請求傳來一個url的時候,會先通過rule找到endpoint(url_map),然後再根據endpoint再找到對應的view_func(view_functions)。通常,endpoint的名字都和檢視函式名一樣。
這時候,這個endpoint也就好理解了:
實際上這個endpoint就是一個Identifier,每個檢視函式都有一個endpoint,
當有請求來到的時候,用它來知道到底使用哪一個檢視函式
在實際應用中,當我們需要在一個檢視中跳轉到另一個檢視中的時候,我們經常會使用url_for(endpoint)去查詢檢視,而不是把地址硬編碼到函式中。
這個時候,我們就不能使用檢視函式名當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
這時候,我們發現,/article這個路由對應了兩個函式名一樣的函式,分別在兩個藍圖中。當我們使用url_for(article)呼叫的時候(注意,url_for是通過endpoint查詢url地址,然後找檢視函式),flask無法知道到底使用哪個藍圖下的endpoint,所以我們需要這樣:
url_for('user.article')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。