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
Process:
Create Rule/path
Assign endpoint to Rule/path
Specify view function for Rule/path.
Key points
When a request comes in, it is located inside the app based on rule/path, that is to say: rule/path 来定位到 app 内部的,也就是说:
不是。正如上面 重点 处所说,如果没有显式指定 endpoint,flask 会将视图函数的名称也即此处的 index 当作此路由规则的 endpoint。
3 . 『为什么需要endpoint参数的时候,需要把视图函数的名称传进去?』
并非是 视图函数的名称,其实是 endpoint
When the user requests /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. 🎜
🎜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
Concept
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
Process:
Create Rule/path
Assign endpoint to Rule/path
Specify view function for Rule/path.
Key points
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。2 . 『endpoint 是一个附加到视图函数的名称,所以,endpoint名就是视图函数的名称么?』
不是。正如上面 重点 处所说,如果没有显式指定 endpoint,flask 会将视图函数的名称也即此处的
index
当作此路由规则的 endpoint。3 . 『为什么需要endpoint参数的时候,需要把视图函数的名称传进去?』
并非是 视图函数的名称,其实是
When the user requestsendpoint
/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
andendpoint
are unique in the routing rule table🎜 🎜 🎜🎜Especiallyendpoint
, 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 orapp.add_url_rule()
function call, flask will specify it for this 🎜Rule rule🎜 A default endpoint, which is the name of thisview function
. 🎜🎜 🎜 🎜Relationship🎜 🎜 🎜🎜Aview function
can have multipleendpoint
andrule
. It is a one-to-many relationship. 🎜🎜 🎜🎜Conversely, anendpoint
can only have onerule
, and can only have oneview 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 isindex
, because it is not displayed in the@app.route
function call🎜 The formula 🎜 specifies the endpoint. 🎜 🎜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 theindex
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 ofendpoint
. It just happens that at some point, the name of endpoint is the same as the name of the 🎜view function🎜. 🎜 🎜Demo🎜 rrreeeLook at this piece of code
We can use classes to write routes. Flask abstracts this writing method into decorators, function name = endpoint
I don’t know if this answer can help you understand the endpoint http://segmentfault.com/q/1010000002980493