Python Flask:获取视图中蓝图的url_前缀
我有一张只有一个视图的蓝图。我想获取blueprint内部视图的url_前缀。不幸的是,test.url\u前缀返回无。还有别的办法吗Python Flask:获取视图中蓝图的url_前缀,python,flask,Python,Flask,我有一张只有一个视图的蓝图。我想获取blueprint内部视图的url_前缀。不幸的是,test.url\u前缀返回无。还有别的办法吗 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
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中,当前视图的路由路径包含在请求变量的url_规则.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)
任务url的值为:
/test/task
如所愿
from flask import Blueprint
admin_panel = Blueprint('admin', __name__, template_folder='templates', static_folder='static')
@admin_panel.route('/')
def index():
url_prefix=admin_panel.name
print(url_prefix)
pass