黄舟2017-04-17 16:22:02
Routing rule table url_map._rules
Rule One of the rules
Function / view function - A view function that handles a certain endpoint/path. It can be simply understood as a function that processes a certain/group of URLs.
Endpoint - flask internal identifier
Others/Rule rule converters, methods and the like
Create Rule/path
Assign endpoint to Rule/path
Specify view function for Rule/path.
When a request comes in, it is located inside the app based on rule/path
, that is to say: rule/path
来定位到 app 内部的,也就是说:
当用户请求 /a
时,会调用 /a
条路由规则指定的视图函数来处理这个请求。
同理,/users/<string:username>
也是一样。
在 flask 的 url_map 路由表中,rule
/path
和 endpoint
在路由规则表里都是唯一的
尤其是 endpoint
,如果重复会报错。
rule/path
,虽然可以重复,但其实只有第一条会生效。
如果开发者没有在 @app.route
装饰器或 app.add_url_rule()
函数调用处指定 endpoint 的话,flask 会为这条 Rule 规则 指定一个默认的 endpoint,即这个 view function
的名字。
一个 view function
,可以有多个 endpoint
、rule
。是个一对多的关系。
反过来,一个 endpoint
,只能有一个 rule
, 也只能有一个 view function
。
现在,回头来看题主的疑问。
1 . 『路由的内部名字是什么?』
名字是:index
。
在题主所说的下面的这个视图中,endpoint/路由规则表内部名称是 index
,因为并未在 @app.route
函数调用中显式指定 endpoint。
@app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if form.validate_on_submit():
session['name'] = form.name.data
return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name'))
2 . 『endpoint 是一个附加到视图函数的名称,所以,endpoint名就是视图函数的名称么?』
不是。正如上面 重点 处所说,如果没有显式指定 endpoint,flask 会将视图函数的名称也即此处的 index
当作此路由规则的 endpoint。
3 . 『为什么需要endpoint参数的时候,需要把视图函数的名称传进去?』
并非是 视图函数的名称,其实是 endpoint
/a
, the view function specified by the /a
routing rule will be called to handle the request.
🎜🎜Similarly, the same goes for /users/<string:username>
. 🎜🎜
🎜
🎜
🎜
🎜In flask's url_map routing table, rule
/path
and endpoint
are unique in the routing rule table🎜
🎜
🎜🎜Especially endpoint
, if it is repeated, an error will be reported. 🎜🎜
🎜🎜rule/path
, although it can be repeated, only the first one will take effect. 🎜🎜
🎜
🎜
🎜🎜If the developer does not specify an endpoint in the @app.route
decorator or app.add_url_rule()
function call, flask will specify it for this 🎜Rule rule🎜 A default endpoint, which is the name of this view function
. 🎜🎜
🎜
🎜Relationship🎜
🎜
🎜🎜A view function
can have multiple endpoint
and rule
. It is a one-to-many relationship. 🎜🎜
🎜🎜Conversely, an endpoint
can only have one rule
, and can only have one view function
. 🎜🎜
🎜
🎜Answers and Explanations🎜
🎜Now, let’s look back at the subject’s question. 🎜
🎜1. "What is the internal name of the route?" 』🎜
🎜The name is: index
. 🎜
🎜In the view below mentioned by the questioner, the internal name of the endpoint/routing rule table is index
, because it is not displayed in the @app.route
function call🎜 The formula 🎜 specifies the endpoint. 🎜
#!/usr/bin/env python3
# coding=utf-8
import flask
app = flask.Flask(__name__)
@app.route('/', endpoint="home")
def amihome():
'''
请尝试以 `/` 和 `/shajiquan` 两个路径来访问;
'''
return "View function: {view}. Endpoint: {endpoint}".format(view="amihome", endpoint=flask.request.endpoint)
# 给 app 添加一条 url rule, 指定 rule, endpoint, 但不指定 view function.
app.add_url_rule(rule='/shajiquan', endpoint="shajiquan", methods=["GET", "DELETE"])
# 为 endpoint="shajiquan" 指定 view function
app.view_functions['shajiquan'] = amihome
@app.route('/')
def miao():
return "wu at: {}".format(flask.request.endpoint)
# 尝试取消注释
# app.view_functions['home'] = miao
if __name__ == '__main__':
app.run(debug=True, port=8964)
🎜2. 『Endpoint is a name attached to the view function, so is the endpoint name the name of the view function? 』🎜
🎜No. As mentioned in the 🎜Key Point🎜 above, if there is no 🎜explicit🎜specify the endpoint, flask will use the name of the view function, which is the index
here, as the endpoint of this routing rule. 🎜
🎜3. "Why do we need to pass the name of the view function when we need the endpoint parameter?" 』🎜
🎜 is not the name of the 🎜view function🎜, but actually the name of endpoint
. It just happens that at some point, the name of endpoint is the same as the name of the 🎜view function🎜. 🎜
🎜Demo🎜
rrreee大家讲道理2017-04-17 16:22:02
Look at this piece of code
class LoginView(views.MethodView):
def get(self):
pass
def post(self):
pass
app.add_url_rule(
'/login/',
endpoint='login',
view_func=LoginView.as_view(b'login'),
methods=['POST', 'GET']
)
We can use classes to write routes. Flask abstracts this writing method into decorators, function name = endpoint
高洛峰2017-04-17 16:22:02
I don’t know if this answer can help you understand the endpoint http://segmentfault.com/q/1010000002980493