flask使用重定向的方式返回上一个页面

2018-11-11  本文已影响0人  LanceAdd
@app.route('/hello')
def hello():
return "hello"

@app.route('/do_something_and_redirect')
def do_something():
    return redirect_back()

def redirect_back(default='hello', **kwargs):
    for target in request.args.get('next'), request.referrer:
        if not target:
            continue
        if is_safe_url(target):
            return redirect(target)
    return redirect(url_for(default, **kwargs))

def is_safe_url(target):
    ref_url = urlparse(request.host_url)
    test_url = urlparse(urljoin(request.host_url, target))
    return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc
上一篇下一篇

猜你喜欢

热点阅读