5

假设我有以下结构:

/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 ( ..) 而言呢?

4

1 回答 1

8

您可以将url_for端点函数的名称传递给/root/.

例如,如果您有:

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

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

redirect(url_for('rootindex'))

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

于 2013-08-05T03:56:57.807 回答