zoukankan      html  css  js  c++  java
  • 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')))

    为了覆盖更全面,可以将查询参数next和referrer两种方式结合起来使用:

    先获取next参数,如果为空就尝试获取referer,如果仍然为空,就重定向到默认的hello视图

    因为在不同视图执行这部分操作的代码相同,我们可以创建一个通用的函数redirect_back()函数

    在do_something视图中调用这个函数

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

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

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

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

     

     
  • 相关阅读:
    linux安装mongodb数据库./mongod -f mongodb.conf失败或者error
    idea项目提交
    spring5.x日志问题log4j
    IDEA使用apache-tomcat-9.0.16控制台Tomcat日志中文乱码问题
    笔记本网线连服务器内网ip配置
    vue项目启动操作
    win10窗口打开一直处于最小化状态问题
    停止 WinDefend Microsoft Defender Antivirus Service
    关于apt命令和yum命令区别
    20200924-4 代码规范
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/10403810.html
Copyright © 2011-2022 走看看