繁体   English   中英

使用来自before_request的字符串(作为路由)调用烧瓶蓝图路由

[英]Call a Flask Blueprint route using a string (as route) from before_request

我需要使用python代码实现以下目标:每当有新请求(例如/ someurl1)到来时,它都应使用代码中以下给出的URL调用category_route。

@app.before_request
def before_request():
    url = '/category/c6f8f568-0a25-4ea7-999c-20099effa5f1'
    # Now here I need a piece of code which calls the below route's method, with above url.


@category.route('/category/<category_id>', methods=['GET'])
def category_route(category_id):
    return {"status": "in category"}

该页面现在应该被重定向。

如果要重定向,则可以使用redirect 我建议您不要使用before_request

@app.route('/someurl1')
def someurl():
    url = '/category/c6f8f568-0a25-4ea7-999c-20099effa5f1'
    return redirect(url)

@category.route('/category/<category_id>', methods=['GET'])
def category_route(category_id):
    return {"status": "in category"}

如果您实际上不想重定向,而只是返回另一个视图的返回值,则可以直接调用该函数:

@app.before_request
def before_request():
    category_id = 'c6f8f568-0a25-4ea7-999c-20099effa5f1'
    return category_route(category_id)

@category.route('/category/<category_id>', methods=['GET'])
def category_route(category_id):
    return {"status": "in category"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM