flask 重定向到上一个页面,referrer、next参数

重定向会上一个页面

在某些场景下,我们需要在用户访问某个url后重定向会上一个页面,比如用户点击某个需要登录才能访问的连接,这时程序会重定向到登录页面,当用户登录后比较合理的行为是重定向到用户登录前浏览的页面。

下面的例中,在foo和bar视图中生成连接,链接过去后,没有重定向会上一个页面


@app.route('/foo')
def foo():
    return '<h1>Foo page </h1><a href="%s">Do something</a>' %url_for('do_something')

@app.route('/bar')
def bar():
    return '<h1>Bar page</h1><a href="%s">Do something </a>' % url_for('do_something')

@app.route('/do_something')
def do_something():
    return redirect(url_for('hello'))

@app.route('/hello')
def hello():
    name = request.args.get('name')
    if name is None:
        name = request.cookies.get('name','xiaxiaoxu')#从cookie中获取name值
       
response = '<h1>Hello, %s</h1>' % name
    return response

if __name__ == '__main__':
    app.run(debug = True)

结果:

访问127.0.0.1:5000/foo或者127.0.0.1:5000/bar后,页面出现连接,点击链接后,进入hello页面,之后停留在了hello页面

点击链接后重定向到了hello页面

我们的目的是在链接后,返回原来的页面

重定向会上一个页面,关键是获取上一个页面的URL。

获取上一个页面的URL有两种方法:

HTTP referrer

HTTP referrer是一个用来记录请求发源地址的HTTP首部字段(HTTP_REFERER),即访问来源。当用户在某个站点点击链接,浏览器想新链接所在的服务器发起请求,请求的数据中包含的HTTP_REFERER字段记录了用户所在的原站点URL。

在flask中,referer的值可以通过请求对象的referrer属性获取,即request.referrer

修改do_something视图函数:

@app.route('/do_something')
def do_something():
    return redirect(request.referrer)

在bar页面上再次点击链接

有的时候,referrer字段可能是空值,比如用户直接在浏览器地址栏输入URL或者因为防火墙或者浏览器设置自动清除或修改referer字段,我们需要添加一个备选项:

return redirect(request.referrer or url_for('hello'))

查询参数next

除了自动从referrer获取,另一种更常见的方式是在URL中手动加入包含当前页面URL的查询参数,这个查询参数一般命名为next

在bar视图中的链接do_something对应的视图添加next参数(在/do_someghing后加参数)

def bar():
    #print dir(request)
   
print "request.full_path:",request.full_path
    #print "request.url:",request.url
   
return '<h1>Bar page</h1><a href="%s">Do something and redirect </a>' % url_for('do_something', next = request.full_path)

@app.route('/do_something')
def do_something():
    return redirect(request.args.get('next'))

为了避免next参数为空的情况,也可以加备选项,如果为空就重定向到hello视图

return redirect(request.args.get('next', url_for('hello')))

猜你喜欢

转载自www.cnblogs.com/xiaxiaoxu/p/10403810.html