简体   繁体   English

@app.route() 的“端点”参数是什么?

[英]What is the "endpoint" parameter for @app.route() used for?

In Flask, you create routes like this:在 Flask 中,您可以像这样创建路由:

from flask import Flask

app = Flask(__name__)

@app.route("/foobar")
def foo_bar_endpoint():
    ...

But the route decorator also has an endpoint parameter.但是路由装饰器也有一个endpoint参数。 The documentation just states:文件只是说明:

the endpoint for the registered URL rule.已注册 URL 规则的端点。 Flask itself assumes the name of the view function as endpoint Flask 本身假定视图函数的名称作为端点

Why should / would one not simply name the function as the endpoint should be called?为什么应该/不会简单地将函数命名为应该调用的端点? Is this relevant for anything else than url_for ?这与url_for以外的其他内容有关吗?

The "endpoint" is an arbitrary name you provide, which is basically the canonical thing that a route refers to. “端点”是您提供的任意名称,它基本上是路由所指的规范事物。 You're not actually mapping a URL to a function there, you're mapping a URL to an endpoint and are attaching a view function to that endpoint.您实际上并没有将 URL 映射到那里的函数,而是将 URL 映射到端点并将视图函数附加到该端点。

URL → endpoint → function

You're just doing all that in one action, and the name of the function automatically becomes the name of the endpoint in the process.您只需在一个操作中完成所有操作,函数的名称将自动成为流程中端点的名称。 But you could do this all separately and register an endpoint for a URL, and later attach a function to that endpoint:但是你可以单独完成这一切并为 URL 注册一个端点,然后将一个函数附加到该端点:

Basically this example:基本上这个例子:

 @app.route('/') def index(): pass

Is equivalent to the following:等效于以下内容:

 def index(): pass app.add_url_rule('/', 'index', index)

If the view_func is not provided you will need to connect the endpoint to a view function like so:如果未提供view_func则需要将端点连接到视图函数,如下所示:

 app.view_functions['index'] = index

https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.add_url_rule https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.add_url_rule

You may want to name your endpoint independently of your function, if you're under some constraint for the function name but are already referring to the endpoint from elsewhere, especially during refactoring, as one possible example where that might become relevant.您可能希望独立于您的函数命名您的端点,如果您受到函数名称的一些限制,但已经从其他地方引用端点,特别是在重构期间,作为一个可能变得相关的可能示例。

The URL is for external clients, for HTTP-requests. URL用于外部客户端,用于 HTTP 请求。

The endpoint is for internal usage if you need to know which URL to redirect for example without any hardcoding.如果您需要知道要重定向的 URL,例如无需任何硬编码,则endpoint供内部使用。

In case you not defining endpoint your code looks like:如果您没有定义端点,您的代码如下所示:

blueprint = Blueprint("bp_1", __name__)


@blueprint.route("/foobar")
def foo_bar_endpoint():
    ...


url = flask.url_for("bp_1.foo_bar_endpoint")

In case you define endpoint, you use function url_for with its name:如果您定义端点,则使用函数url_for及其名称:

@blueprint.route("/foobar", endpoint="custom_name")
def foo_bar_endpoint():
    ...


url = flask.url_for("bp_1. custom_name")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM