本文譯自: https://stackoverflow.com/questions/19261833/what-is-an-endpoint-in-flask
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.
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
那么什么是 endpoint 呢肖油?
Flask中路由Routing的工作方式
Flask(和底層的Werkzeug庫)的基本思想是將URL路徑映射到一些你將運(yùn)行的邏輯(通常是“視圖函數(shù)”)兼吓。 通常你的視圖函數(shù)是這樣定義的:
@app.route('/greeting/<name>')
def give_greeting(name):
return 'Hello, {0}!'.format(name)
注意函數(shù) add_url_rule 也實現(xiàn)了相同的目標(biāo),只是不使用裝飾器森枪。 所以下面是一樣的:
# No "route" decorator here. We will add routing using a different method below.
def give_greeting(name):
return 'Hello, {0}!'.format(name)
app.add_url_rule('/greeting/<name>', 'give_greeting', give_greeting)
假設(shè)你的網(wǎng)站位于“www.example.org”并使用上述視圖视搏。 用戶在瀏覽器中輸入以下URL:
http://www.example.org/greeting/Mark
Flask的工作是獲取這個URL审孽,找出用戶想要做的事情,然后把它傳遞給你眾多python函數(shù)中的一個來處理浑娜。 它采取的路徑是:
/greeting/Mark
并將其匹配到路由列表佑力。 在我們的例子中,我們定義了這個路徑和give_greeting函數(shù)匹配筋遭。
然而打颤,雖然這是您創(chuàng)建視圖的典型方式,但它實際上隱藏了一些細(xì)節(jié)漓滔。 實際上编饺,F(xiàn)lask并沒有直接從URL到應(yīng)該處理這個請求的視圖函數(shù)。 它并不是簡單的做如下處理:
URL (http://www.example.org/greeting/Mark) should be handled by View Function (the function "my_greeting")
實際上响驴,還有一個步驟---將URL映射到一個端點:
URL (http://www.example.org/greeting/Mark) should be handled by Endpoint "my_greeting".
Requests to Endpoint "my_greeting" should be handled by View Function "my_greeting"
“endpoint” 是用于確定 Request 該由代碼的哪個邏輯單元來處理透且。 通常情況下,endpoint 只是一個視圖函數(shù)的名稱豁鲤。 但是秽誊,實際上可以更改端點,如下例所示:
@app.route('/greeting/<name>', endpoint='say_hello')
def give_greeting(name):
return 'Hello, {0}!'.format(name)
現(xiàn)在畅形,當(dāng)Flask路由請求時养距,邏輯如下所示:
URL (http://www.example.org/greeting/Mark) should be handled by Endpoint "say_hello".
Endpoint "say_hello" should be handled by View Function "my_greeting"
如何使用endpoint
端點通常用于“反向查找”。 例如日熬,在Flask應(yīng)用程序的一個視圖函數(shù)中想要引用另一個視圖函數(shù)棍厌。 可以使用 url_for() ,而不是直接使用URL竖席。 示例如下:
@app.route('/')
def index():
print url_for('give_greeting', name='Mark') # This will print '/greeting/Mark'
@app.route('/greeting/<name>')
def give_greeting(name):
return 'Hello, {0}!'.format(name)
這是有利的耘纱,因為現(xiàn)在我們可以改變我們的應(yīng)用程序的URL而不需要改變我們引用該資源的那一行。
為什么不直接用視圖函數(shù)呢毕荐?
現(xiàn)在的問題是:“為什么我們需要這個額外的層 (endpoint) 束析? 為什么要將路徑映射到endpoint,然后將endpoint映射到視圖函數(shù)憎亚? 為什么不跳過這個中間步驟员寇?
原因是因為這種方式更強(qiáng)大。 例如第美,F(xiàn)lask Blueprints允許您將應(yīng)用程序分成不同的部分蝶锋。 我可能將所有管理端資源都放在名為“admin”的 blueprint 中,并將所有用戶級資源放在“user”的 endpoint中什往。
Blueprints 允許您將這些分隔到命名空間中扳缕。 例如...
main.py:
from flask import Flask, Blueprint
from admin import admin
from user import user
app = Flask(__name__)
app.register_blueprint(admin, url_prefix='admin')
app.register_blueprint(user, url_prefix='user')
admin.py:
admin = Blueprint('admin', __name__)
@admin.route('/greeting')
def greeting():
return 'Hello, administrative user!'
user.py:
user = Blueprint('user', __name__)
@user.route('/greeting')
def greeting():
return 'Hello, lowly normal user!'
請注意,在這兩個blueprint中,“/ greeting”路由是一個名為“greeting”的函數(shù)躯舔。 如果我想引用admin 的 “greeting” 函數(shù)驴剔,我們需要使用 "admin.greeting"。 endpoint 允許我們將blueprint的名稱指定為端點的一部分粥庄,從而實現(xiàn)一種命名空間丧失。 所以,我們可以使用下面的做法:
print url_for('admin.greeting') # Prints '/admin/greeting'
print url_for('user.greeting') # Prints '/user/greeting'