该瓶文档显示:
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
Run Code Online (Sandbox Code Playgroud)
"终点"究竟是什么意思?
Mar*_*eth 238
Flask(以及底层的Werkzeug库)的整个想法是将URL路径映射到您将运行的某些逻辑(通常是"视图函数").您的基本视图定义如下:
@app.route('/greeting/<name>')
def give_greeting(name):
return 'Hello, {0}!'.format(name)
Run Code Online (Sandbox Code Playgroud)
请注意,您引用的函数(add_url_rule)实现了相同的目标,只是不使用装饰符号.因此,以下是相同的:
# 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)
Run Code Online (Sandbox Code Playgroud)
假设您的网站位于"www.example.org"并使用上述视图.用户在其浏览器中输入以下URL:
http://www.example.org/greeting/Mark
Run Code Online (Sandbox Code Playgroud)
Flask的工作是获取此URL,找出用户想要做的事情,并将其传递给您的许多python函数之一进行处理.需要路径:
/greeting/Mark
Run Code Online (Sandbox Code Playgroud)
...并将其与路线列表相匹配.在我们的例子中,我们定义了这个路径来转到give_greeting
函数.
但是,虽然这是您创建视图的典型方式,但实际上它会从您那里抽象出一些额外的信息.在幕后,Flask没有直接从URL跳转到应该处理此请求的view函数.它并不是简单地说......
URL (http://www.example.org/greeting/Mark) should be handled by View Function (the function "my_greeting")
Run Code Online (Sandbox Code Playgroud)
实际上,还有另一个步骤,它将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"
Run Code Online (Sandbox Code Playgroud)
基本上,"端点"是用于确定代码的哪个逻辑单元应该处理请求的标识符.通常,端点只是视图函数的名称.但是,您可以实际更改端点,如以下示例中所示.
@app.route('/greeting/<name>', endpoint='say_hello')
def give_greeting(name):
return 'Hello, {0}!'.format(name)
Run Code Online (Sandbox Code Playgroud)
现在,当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"
Run Code Online (Sandbox Code Playgroud)
端点通常用于"反向查找".例如,在Flask应用程序的一个视图中,您想要引用另一个视图(可能在您从站点的一个区域链接到另一个区域时).您可以使用而不是对URL进行硬编码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)
Run Code Online (Sandbox Code Playgroud)
这是有利的,因为现在我们可以更改应用程序的URL,而无需更改引用该资源的行.
可能出现的一个问题是:"为什么我们需要这个额外的层?" 为什么要将路径映射到端点,然后将端点映射到视图函数?为什么不跳过那个中间步骤呢?
原因是因为它以这种方式更强大.例如,Flask Blueprints允许您将应用程序拆分为各个部分.我可能将所有管理员资源都放在一个名为"admin"的蓝图中,以及一个名为"user"的端点中的所有用户级资源.
蓝图允许您将这些名称分隔为名称空间.例如...
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')
Run Code Online (Sandbox Code Playgroud)
admin.py:
admin = Blueprint('admin', __name__)
@admin.route('/greeting')
def greeting():
return 'Hello, administrative user!'
Run Code Online (Sandbox Code Playgroud)
user.py:
user = Blueprint('user', __name__)
@user.route('/greeting')
def greeting():
return 'Hello, lowly normal user!'
Run Code Online (Sandbox Code Playgroud)
请注意,在两个蓝图中,'/ greeting'路径是一个名为"greeting"的函数.如果我想参考管理员"问候"功能,我不能只说"问候",因为还有一个用户"问候"功能.端点允许您指定蓝图的名称作为端点的一部分,从而允许一种命名空间.所以,我可以做以下......
print url_for('admin.greeting') # Prints '/admin/greeting'
print url_for('user.greeting') # Prints '/user/greeting'
Run Code Online (Sandbox Code Playgroud)
pla*_*aes 21
端点是用于反向查找URL规则url_for
的名称,默认为视图功能的名称.
小例子:
from flask import Flask, url_for
app = Flask(__name__)
# We can use url_for('foo_view') for reverse-lookups in templates or view functions
@app.route('/foo')
def foo_view():
pass
# We now specify the custom endpoint named 'bufar'. url_for('bar_view') will fail!
@app.route('/bar', endpoint='bufar')
def bar_view():
pass
with app.test_request_context('/'):
print url_for('foo_view')
print url_for('bufar')
# url_for('bar_view') will raise werkzeug.routing.BuildError
print url_for('bar_view')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
40267 次 |
最近记录: |