假设我有以下结构:
/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 (..
) 而言?
最佳答案
您可以将 /root/
的端点函数名称传递给 url_for
。
例如,如果您有:
@app.route('/root/')
def rootindex():
return "this is the index for the whole site."
在您应用的其他地方,您可以:
redirect(url_for('rootindex'))
在这里引起重定向。当您将 .
放在传递给 url_for
的字符串前面时,这会告诉它在当前蓝图中查找端点。通过关闭 .
,您告诉它在 app
关于python - 如何重定向到 flask 蓝图父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18049934/