在flask框架中,我們經(jīng)常會(huì)遇到endpoint這個(gè)東西,最開始也沒法理解這個(gè)到底是做什么的。最近正好在研究Flask的源碼,也就順帶了解了一下這個(gè)endpoint
首先,我們看一個(gè)例子:
@app.route('/user/')defuser(name):return'Hello,%s'%name
這個(gè)是我們?cè)谟胒lask框架寫網(wǎng)站中最常用的。
通過看源碼,我們可以發(fā)現(xiàn):
函數(shù)等效于
defuser(name)return'Hello,%s'%nameapp.add_url_rule('/user/','user', user)
這個(gè)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這個(gè)參數(shù)不談,我們看看前三個(gè)參數(shù)。
rule:這個(gè)參數(shù)很簡單,就是匹配的路由地址
view_func:這個(gè)參數(shù)就是我們寫的視圖函數(shù)
endpoint:這個(gè)參數(shù)就是我今天重點(diǎn)要講的,endpoint
很多人認(rèn)為:假設(shè)用戶訪問http://www.example.com/user/eric,flask會(huì)找到該函數(shù),并傳遞name='eric',執(zhí)行這個(gè)函數(shù)并返回值。
但是實(shí)際中,F(xiàn)lask真的是直接根據(jù)路由查詢視圖函數(shù)么?
在源碼中我們可以發(fā)現(xiàn):
每個(gè)應(yīng)用程序app都有一個(gè)view_functions,這是一個(gè)字典,存儲(chǔ)endpoint-view_func鍵值對(duì)。add_url_rule的第一個(gè)作用就是向view_functions中添加鍵值對(duì)(這件事在應(yīng)用程序run之前就做好了)
每個(gè)應(yīng)用程序app都有一個(gè)url_map,它是一個(gè)Map類(具體實(shí)現(xiàn)在werkzeug/routing.py中),里面包含了一個(gè)列表,列表元素是Role的實(shí)例(werkzeug/routing.py中)。add_url_rule的第二個(gè)作用就是向url_map中添加Role的實(shí)例(它也是在應(yīng)用程序run之前就做好了)
我們可以通過一個(gè)例子來看:
app=Flask(__name__)@app.route('/test', endpoint='Test')deftest():pass@app.route('/', endpoint='index')defhello_world():return'Hello World!'if__name__=='__main__':print(app.view_functions)print(app.url_map)? ? app.run()
運(yùn)行這個(gè)程序,結(jié)果是:
{'static': >, 'Test': , 'index': }Map([ Test>,
index>,
' (HEAD, OPTIONS, GET) -> static>]) * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
所以我們可以看出:這個(gè)url_map存儲(chǔ)的是url與endpoint的映射!
回到flask接受用戶請(qǐng)求地址并查詢函數(shù)的問題。實(shí)際上,當(dāng)請(qǐng)求傳來一個(gè)url的時(shí)候,會(huì)先通過rule找到endpoint(url_map),然后再根據(jù)endpoint再找到對(duì)應(yīng)的view_func(view_functions)。通常,endpoint的名字都和視圖函數(shù)名一樣。
這時(shí)候,這個(gè)endpoint也就好理解了:
實(shí)際上這個(gè)endpoint就是一個(gè)Identifier,每個(gè)視圖函數(shù)都有一個(gè)endpoint,
當(dāng)有請(qǐng)求來到的時(shí)候,用它來知道到底使用哪一個(gè)視圖函數(shù)
在實(shí)際應(yīng)用中,當(dāng)我們需要在一個(gè)視圖中跳轉(zhuǎn)到另一個(gè)視圖中的時(shí)候,我們經(jīng)常會(huì)使用url_for(endpoint)去查詢視圖,而不是把地址硬編碼到函數(shù)中。
這個(gè)時(shí)候,我們就不能使用視圖函數(shù)名當(dāng)endpoint去查詢了
我們舉個(gè)例子來說明。比如:
app=Flask(__name__)app.register_blueprint(user, url_prefix='user')app.register_blueprint(file, url_prefix='file')
我們注冊(cè)了2個(gè)藍(lán)圖。
在user中(省略初始化過程):
@user.route('/article')defarticle():pass
在file中(省略初始化過程):
@file.route('/article')defarticle():pass
這時(shí)候,我們發(fā)現(xiàn),/article這個(gè)路由對(duì)應(yīng)了兩個(gè)函數(shù)名一樣的函數(shù),分別在兩個(gè)藍(lán)圖中。當(dāng)我們使用url_for(article)調(diào)用的時(shí)候(注意,url_for是通過endpoint查詢url地址,然后找視圖函數(shù)),flask無法知道到底使用哪個(gè)藍(lán)圖下的endpoint,所以我們需要這樣:
url_for('user.article')