request.environ ['HTTP_REFERER']是None [英] request.environ['HTTP_REFERER'] is None

查看:508
本文介绍了request.environ ['HTTP_REFERER']是None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python的框架中得到 HTTP_REFERER
我的路线是这样的:

$ p $ @ app.route('/ login')

)def login():
$ b $如果authenticateForPanel():
返回重定向(url_for(panel))
else:
ref = request.environ [ HTTP_REFERER']

return render_template('login.html',blogOptions = g.blogOptions,ref = ref)

当我执行这个时,我得到了 KeyError:'HTTP_REFERER'跟踪:

  Traceback(最近一次调用的最后一个):
文件/Users/ozcan/flask/flask/app.py,第1823行,在__call__
返回self.wsgi_app(environ,start_response)
文件/Users/ozcan/flask/flask/app.py,行1811,在wsgi_app
response = self.make_response(self.handle_exception(e))
在wsgi_app
response = self.full_dispatch_request()
文件/Users/ozcan/flask/flask/app.py,第1809行,文件/ Users / ozcan / flask / flask /app.py,第1482行,在full_di中spatch_request
rv = self.handle_user_exception(e)
文件/Users/ozcan/flask/flask/app.py,1480行,在full_dispatch_request
rv = self.dispatch_request()
文件/Users/ozcan/flask/flask/app.py,1466行,在dispatch_request
返回self.view_functions [rule.endpoint](** req.view_args)
文件 /Users/ozcan/Documents/python/app.py,第318行,登录
ref = request.environ ['HTTP_REFERER']
KeyError:'HTTP_REFERER'

当我第一次写这段代码的时候,它是工作的。我不直接调用这个url。我调用 localhost:5000 / panel ,它将我重定向到 login 方法。所以基本上应该有一个referer,我错了吗?

当我打印 request.environ ['HTTP_REFERER'] 时,打印 None



我也尝试过使用

ref = request.referrer 但它是 None



为什么它可以发生这种情况的原因是:

解决方案

werkzeug请求包装,
$ b $ h $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

  from flask import Flask,request 
import unittest
$ b $ app = Flask(__ name__)
@ app.route(' /')
def index():
return unicode(request.referrer)
$ b $ class Test(unittest.TestCase):

def test( self):
c = app.test_client()
resp = c.get('/',headers = {'Referer':'/ somethingelse'})
self.assertEqual('如果是______ =='__main__':
unittest.main()

但是,在更一般的情况下,如果键不存在,要检索指定默认值的字典键的值,请使用 dict.get


i want to get HTTP_REFERER in python flask framework. My route is this:

@app.route('/login')

def login():

    if authenticateForPanel():
        return redirect(url_for("panel"))
    else:               
        ref = request.environ['HTTP_REFERER']

        return render_template('login.html',blogOptions = g.blogOptions,ref=ref)

When i execute this,i got KeyError: 'HTTP_REFERER' with the traceback:

Traceback (most recent call last):
  File "/Users/ozcan/flask/flask/app.py", line 1823, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/ozcan/flask/flask/app.py", line 1811, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/ozcan/flask/flask/app.py", line 1809, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/ozcan/flask/flask/app.py", line 1482, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/ozcan/flask/flask/app.py", line 1480, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/ozcan/flask/flask/app.py", line 1466, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/ozcan/Documents/python/app.py", line 318, in login
    ref = request.environ['HTTP_REFERER']
KeyError: 'HTTP_REFERER'

When i first wrote this code it was working.I do not directly call this url.I call localhost:5000/panel and it redirects me to the login method.So basically there should be a referer,am i wrong?

When i print the request.environ['HTTP_REFERER'] it prints None

I also tried with the

ref = request.referrer but it is None

Why it can be happen?Thanks a lot.

解决方案

The werkzeug Request wrapper, which flask uses by default, does this work for you: request.referrer

from flask import Flask, request
import unittest

app = Flask(__name__)
@app.route('/')
def index():
    return unicode(request.referrer)

class Test(unittest.TestCase):

    def test(self):
        c = app.test_client()
        resp = c.get('/', headers={'Referer': '/somethingelse'})
        self.assertEqual('/somethingelse', resp.data)

if __name__ == '__main__':
    unittest.main()

But in the more general case, to retrieve the value of a dictionary key specifying a default if the key is not present, use dict.get

这篇关于request.environ ['HTTP_REFERER']是None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆