使用Flask和非唯一处理程序名称构建URL [英] URL building with Flask and non-unique handler names

查看:259
本文介绍了使用Flask和非唯一处理程序名称构建URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flask提供了一个 url_for 函数根据URL模式为处理程序生成URL。但是这意味着处理函数在整个应用程序中必须有唯一的名字。这是正确的吗?

示例

模块A有一个处理程序<$ c




$ index $($) index():pass

而模块B有另一个处理程序 index $ b $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $

如何区分名为 index 的处理程序网址吗?

  url_for('index')


解决方案

我不知道如何处理由同一个模块路由的所有视图。

我通常做的是在不同的模块中分开我的视图(就像你用模块 A B ) ,然后将它们注册为蓝图,然后在使用 url_for()函数时,可以在视图名称前面加上蓝图名称,避免冲突和潜在的问题。 / p>

以下是一个例子:

main_views.py:



$ b $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ '/')
def index():
pass

admin_views.py:

  from flask import蓝图
admin =蓝图('admin',__name__)

@ admin.route('/ admin')
def ind ex():
pass

application.py:

  from flask导入Flask 
from main_views导入主要$ b $ from admin_views导入管理

app = Flask('my_application')
app.register_blueprint(main)
app.register_blueprint(admin)

现在,要访问2个索引视图,仍然可以区分两个索引视图,只需使用 url_for('main.index') url_for('admin.index')



编辑:



在注册蓝图时,您可以传递一个 url_prefix 参数,这个参数适用于本蓝图中的每个视图。



例如,给出以下代码:

admin_views.py


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ $' /')
def index():
传递

@ admin.route('/ logout')
def logout():
传递

application.py:

从瓶子导入Flask 
从admin_views导入管理
$ b $ app = Flask('my_application')
app.register_blueprint(admin,url_prefix = '/ admin')

2个视图可以在URL / admin / / admin / logout


Flask provides a url_for function to generate URLs to handlers based on the URL pattern. But this would imply that the handler functions must have unique names across the entire application. Is that correct?

Example

Module A has a handler index:

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

And Module B has another handler index:

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

How to distinguish the handlers called index when building URLs?

url_for('index')

解决方案

I don't know how you could do with all the views routed by the same module.

What I usually do is separate my views in different modules (like you did with module A and B), and register them as blueprints, after that, when using the url_for() function, you can prefix the view name with your blueprint name and then avoid conflicts and potential problems.

Here is an example:

main_views.py:

from flask import Blueprint
main = Blueprint('main', __name__)

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

admin_views.py:

from flask import Blueprint
admin = Blueprint('admin', __name__)

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

application.py:

from flask import Flask
from main_views import main
from admin_views import admin

app = Flask('my_application')
app.register_blueprint(main)
app.register_blueprint(admin)

Now, to access the 2 index views and still distinguish one from the other, just use url_for('main.index') or url_for('admin.index')

EDIT:

Just one more useful details about routing using blueprints, when registering the blueprint, you can pass a url_prefix argument, that will apply to every view within this blueprint.

For example, given the following code:

admin_views.py

from flask import Blueprint
admin = Blueprint('admin', __name__)

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

@admin.route('/logout')
def logout():
    pass

application.py:

from flask import Flask
from admin_views import admin

app = Flask('my_application')
app.register_blueprint(admin, url_prefix='/admin')

The 2 views would be available at the URL /admin/ and /admin/logout

这篇关于使用Flask和非唯一处理程序名称构建URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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