Flask 中的“端点"是什么? [英] What is an 'endpoint' in Flask?

查看:18
本文介绍了Flask 中的“端点"是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

端点"究竟是什么意思?

What exactly is meant by an "endpoint"?

推荐答案

Flask Routing 的工作原理

Flask(以及底层的 Werkzeug 库)的整个想法是将 URL 路径映射到您将运行的某些逻辑(通常是视图函数").您的基本视图定义如下:

How Flask Routing Works

The entire idea of Flask (and the underlying Werkzeug library) is to map URL paths to some logic that you will run (typically, the "view function"). Your basic view is defined like this:

@app.route('/greeting/<name>')
def give_greeting(name):
    return 'Hello, {0}!'.format(name)

请注意,您引用的函数 (add_url_rule) 实现了相同的目标,只是没有使用装饰器符号.因此,下面是相同的:

Note that the function you referred to (add_url_rule) achieves the same goal, just without using the decorator notation. Therefore, the following is the same:

# 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)

假设您的网站位于www.example.org"并使用上述视图.用户在浏览器中输入以下 URL:

Let's say your website is located at 'www.example.org' and uses the above view. The user enters the following URL into their browser:

http://www.example.org/greeting/Mark

Flask 的工作是获取这个 URL,弄清楚用户想要做什么,然后将它传递给您的众多 Python 函数之一进行处理.它需要路径:

The job of Flask is to take this URL, figure out what the user wants to do, and pass it on to one of your many python functions for handling. It takes the path:

/greeting/Mark

...并将其与路线列表相匹配.在我们的例子中,我们定义了这条路径以转到 give_greeting 函数.

...and matches it to the list of routes. In our case, we defined this path to go to the give_greeting function.

然而,虽然这是您创建视图的典型方式,但它实际上从您那里提取了一些额外的信息.在幕后,Flask 并没有直接从 URL 跳转到应该处理这个请求的视图函数.它不是简单地说...

However, while this is the typical way that you might go about creating a view, it actually abstracts some extra info from you. Behind the scenes, Flask did not make the leap directly from URL to the view function that should handle this request. It does not simply say...

URL (http://www.example.org/greeting/Mark) should be handled by View Function (the function "give_greeting")

实际上,还有一个步骤,将 URL 映射到端点:

Actually, it there is another step, where it maps the URL to an endpoint:

URL (http://www.example.org/greeting/Mark) should be handled by Endpoint "give_greeting".
Requests to Endpoint "give_greeting" should be handled by View Function "give_greeting"

基本上,端点"是一个标识符,用于确定代码的哪个逻辑单元应该处理请求.通常,端点只是视图函数的名称.但是,您实际上可以更改端点,如下例所示.

Basically, the "endpoint" is an identifier that is used in determining what logical unit of your code should handle the request. Normally, an endpoint is just the name of a view function. However, you can actually change the endpoint, as is done in the following example.

@app.route('/greeting/<name>', endpoint='say_hello')
def give_greeting(name):
    return 'Hello, {0}!'.format(name)

现在,当 Flask 路由请求时,逻辑如下所示:

Now, when Flask routes the request, the logic looks like this:

URL (http://www.example.org/greeting/Mark) should be handled by Endpoint "say_hello".
Endpoint "say_hello" should be handled by View Function "give_greeting"

您如何使用端点

端点通常用于反向查找".例如,在 Flask 应用程序的一个视图中,您想要引用另一个视图(也许当您从站点的一个区域链接到另一个区域时).您可以使用 url_for().假设如下

@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)

这是有利的,因为现在我们可以更改应用程序的 URL,而无需更改引用该资源的行.

This is advantageous, as now we can change the URLs of our application without needing to change the line where we reference that resource.

可能出现的一个问题是:为什么我们需要这个额外的层?"为什么将路径映射到端点,然后将端点映射到视图函数?为什么不直接跳过中间步骤?

One question that might come up is the following: "Why do we need this extra layer?" Why map a path to an endpoint, then an endpoint to a view function? Why not just skip that middle step?

原因是因为这种方式更强大.例如,Flask Blueprints 允许您将应用程序拆分为不同的部分.我可能在名为admin"的蓝图中拥有我的所有管理端资源,而在名为user"的端点中拥有我的所有用户级资源.

The reason is because it is more powerful this way. For example, Flask Blueprints allow you to split your application into various parts. I might have all of my admin-side resources in a blueprint called "admin", and all of my user-level resources in an endpoint called "user".

蓝图允许您将它们分成命名空间.例如...

Blueprints allow you to separate these into namespaces. For example...

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!'

请注意,在两个蓝图中,'/greeting' 路由是一个名为greeting"的函数.如果我想提到管理问候"功能,我不能只说问候",因为还有一个用户问候"功能.通过让您将蓝图的名称指定为端点的一部分,端点允许某种命名空间.所以,我可以做以下...

Note that in both blueprints, the '/greeting' route is a function called "greeting". If I wanted to refer to the admin "greeting" function, I couldn't just say "greeting" because there is also a user "greeting" function. Endpoints allow for a sort of namespacing by having you specify the name of the blueprint as part of the endpoint. So, I could do the following...

print url_for('admin.greeting') # Prints '/admin/greeting'
print url_for('user.greeting') # Prints '/user/greeting'

这篇关于Flask 中的“端点"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆