request.environ['HTTP_REFERER'] is None
Question: 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.


Answer: The werkzeug Request wrapper, which flask uses by default, does this work for
you:
[request.referrer](http://werkzeug.pocoo.org/docs/wrappers/#werkzeug.wrappers.CommonRequestDescriptorsMixin.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](http://docs.python.org/2/library/stdtypes.html#dict.get)

