2013-08-05 32 views
4

假设我有以下结构:我该如何重定向到烧瓶蓝图父项?

/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(这是相对于当前的网址(..)?

回答

6

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

例如,如果您有:

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

在您的应用程序的其他地方,你可以这样做:

redirect(url_for('rootindex')) 

要在这里导致重定向。当您将字符串.放在您传递给url_for的字符串前面时,它会指示它在当前蓝图中查找端点。通过将.关闭,您告诉它寻找app

+1

我认为问题是问是否有一个相对的方法,而不仅仅是一个静态的方式 - 所以如果你有一个蓝图堆叠三个深层次(祖父母 - >父母 - >孩子)你怎么能相对父母的根子功能从孩子(假设你的命名方法是相同的),而不提及父母的名字直接。 – Doobeh

+1

只是为了回答我自己的问题 - 目前你不能嵌套蓝图,尽管它正在研究中。 – Doobeh

+1

事实上,提到一个相对于蓝图父项的路径会很好。但是现在这为我修复了它。 –