Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP referer issue

I have this view rendering a static page, but I cannot seem to catch the referer of the page.

def landing(request, referer='google'):
    ''' Loads the landing page '''
    msg = ''
    if request.method == 'GET':
        referer = request.META['HTTP_REFERER']
        return render_to_response('index.html',
            {'WSGI_DIR': settings.WSGI_DIR,'csrf_value': get_token(request),
                'referer':referer},context_instance=RequestContext(request))

It keeps popping:

KeyError at / 'HTTP_REFERER'

I've imported everything needed. Does anyone have a clue?

like image 536
user823148 Avatar asked Dec 17 '22 09:12

user823148


2 Answers

You should be using request.META.get('HTTP_REFERER'). Not every request will have a Referer header, and if one doesn't you will get exactly this exception. Test if the result of get() is not None to see if the header was sent.

like image 195
cdhowie Avatar answered Jan 01 '23 12:01

cdhowie


Make this change to fix the key error:

referer = request.META.get('HTTP_REFERER', '')
like image 34
Alex Smith Avatar answered Jan 01 '23 12:01

Alex Smith