gpt4 book ai didi

python - Flask:在 View 中获取蓝图的 url_prefix

转载 作者:行者123 更新时间:2023-12-03 17:00:07 25 4
gpt4 key购买 nike

我有一个只有一个 View 的蓝图。我想在 View 中获取蓝图的 url_prefix。不幸的是 test.url_prefix 返回 None。还有其他办法吗?

app.register_blueprint(test_blueprint, url_prefix = "/test")

@test.route("/task", methods=["GET"])
def task_view(user):
task_url = test.url_prefix + "/task" # test.url_prefix is None ??

最佳答案

是的。

在 Flask 中,当前 View 的路由路径包含在 request 变量的 url_rule.rule 子属性中。

因此您可以执行以下操作:

from flask import request

...

test_blueprint = Blueprint('test', __name__, url_prefix='/test')

...

@test_blueprint.route("/task", methods=["GET"])
def task_view(user):
task_url = request.url_rule.rule

....

app.register_blueprint(test_blueprint)

task_url 的值为:

/test/task

根据需要。

关于python - Flask:在 View 中获取蓝图的 url_prefix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41599500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com