假设我有以下结构:
/root/user/login
Run Code Online (Sandbox Code Playgroud)
我在蓝图中登录:
app.register_blueprint(login_blueprint,url_prefix=prefix('/user'))
Run Code Online (Sandbox Code Playgroud)
我可以重定向到".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)
Run Code Online (Sandbox Code Playgroud)
重定向带我到/root/user:
redirect(url_for('.index'))
Run Code Online (Sandbox Code Playgroud)
但是如何到达/root(相对于当前的url(..)是多少?
您可以传递url_for端点函数的名称/root/.
例如,如果您有:
@app.route('/root/')
def rootindex():
return "this is the index for the whole site."
Run Code Online (Sandbox Code Playgroud)
在你的应用程序的其他地方,你可以做:
redirect(url_for('rootindex'))
Run Code Online (Sandbox Code Playgroud)
要在此处进行重定向.当你把一个.字符串放在你传递给的字符串前面时url_for,它会告诉它在当前的蓝图中寻找一个端点.通过离开.,你告诉它寻找一个端点app