如何重定向到Flask蓝图父级?

2024-05-15 00:29:18 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下结构:

/root/user/login

我在蓝图中登录:

  app.register_blueprint(login_blueprint,url_prefix=prefix('/user'))

我可以重定向到“.index”:

@login_blueprint.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        #### this redirects me to '/root/user/'
        redirect_to_index= redirect(url_for('.index'))
        response = current_app.make_response(redirect_to_index)
        # do the log in
     return response

    redirect_to_index=redirect(url_for('.index'))

    response = current_app.make_response(redirect_to_index)

重定向使我转到/root/user

redirect(url_for('.index'))

但是如何到达/root(相对于当前url(..)呢?


Tags: toappurlforprefixindexresponselogin
5条回答

可以向url_for传递/root/的终结点函数的名称。

例如,如果您有:

@app.route('/root/')
def rootindex():
    return "this is the index for the whole site."

在应用程序的其他地方,您可以执行以下操作:

redirect(url_for('rootindex'))

在这里引起重定向。当您将.放在传递给url_for的字符串前面时,这将告诉它在当前蓝图中查找端点。关闭.,告诉它在app中查找端点

相关问题 更多 >

    热门问题