使用Flask和非唯一处理程序名称构建URL [英] URL building with Flask and non-unique handler names
问题描述
Flask提供了一个 url_for
函数根据URL模式为处理程序生成URL。但是这意味着处理函数在整个应用程序中必须有唯一的名字。这是正确的吗?
示例 模块A有一个处理程序<$ c 而模块B有另一个处理程序 如何区分名为 我不知道如何处理由同一个模块路由的所有视图。 我通常做的是在不同的模块中分开我的视图(就像你用模块 以下是一个例子: main_views.py: admin_views.py: application.py: 现在,要访问2个索引视图,仍然可以区分两个索引视图,只需使用 在注册蓝图时,您可以传递一个 例如,给出以下代码: admin_views.py application.py: 2个视图可以在URL Flask provides a Example Module A has a handler And Module B has another handler How to distinguish the handlers called
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 Here is an example: main_views.py: admin_views.py: application.py: Now, to access the 2 index views and still distinguish one from the other, just use Just one more useful details about routing using blueprints, when registering the blueprint, you can pass a For example, given the following code: admin_views.py application.py: The 2 views would be available at the URL 这篇关于使用Flask和非唯一处理程序名称构建URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ index $($) index():pass
index
$ b $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $
index
的处理程序网址吗?
url_for('index')
A
和 B
) ,然后将它们注册为蓝图,然后在使用 url_for()
函数时,可以在视图名称前面加上蓝图名称,避免冲突和潜在的问题。 / p>
$ b $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ '/')
def index():
pass
from flask import蓝图
admin =蓝图('admin',__name__)
@ admin.route('/ admin')
def ind ex():
pass
from flask导入Flask
from main_views导入主要$ b $ from admin_views导入管理
app = Flask('my_application')
app.register_blueprint(main)
app.register_blueprint(admin)
url_for('main.index')
或 url_for('admin.index')
编辑:
url_prefix
参数,这个参数适用于本蓝图中的每个视图。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ $' /')
def index():
传递
@ admin.route('/ logout')
def logout():
传递
从瓶子导入Flask
从admin_views导入管理
$ b $ app = Flask('my_application')
app.register_blueprint(admin,url_prefix = '/ admin')
/ admin /
和 / admin / logout
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?index
: @app.route('/')
def index(): pass
index
: @app.route('/anotherindex')
def index(): pass
index
when building URLs? url_for('index')
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.from flask import Blueprint
main = Blueprint('main', __name__)
@main.route('/')
def index():
pass
from flask import Blueprint
admin = Blueprint('admin', __name__)
@admin.route('/admin')
def index():
pass
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)
url_for('main.index')
or url_for('admin.index')
EDIT:
url_prefix
argument, that will apply to every view within this blueprint.from flask import Blueprint
admin = Blueprint('admin', __name__)
@admin.route('/')
def index():
pass
@admin.route('/logout')
def logout():
pass
from flask import Flask
from admin_views import admin
app = Flask('my_application')
app.register_blueprint(admin, url_prefix='/admin')
/admin/
and /admin/logout