Django request to find previous referrer

DjangoDjango ModelsDjango TemplatesDjango Views

Django Problem Overview


I am passing the request to the template page.In django template how to pass the last page from which the new page was initialised.Instead of history.go(-1) i need to use this

 {{request.http referer}} ??

 <input type="button" value="Back" /> //onlcick how to call the referrer 

Django Solutions


Solution 1 - Django

That piece of information is in the META attribute of the HttpRequest, and it's the HTTP_REFERER (sic) key, so I believe you should be able to access it in the template as:

{{ request.META.HTTP_REFERER }}

Works in the shell:

>>> from django.template import *
>>> t = Template("{{ request.META.HTTP_REFERER }}")
>>> from django.http import HttpRequest
>>> req = HttpRequest()
>>> req.META
{}
>>> req.META['HTTP_REFERER'] = 'google.com'
>>> c = Context({'request': req})
>>> t.render(c)
u'google.com'

Solution 2 - Django

Rajeev, this is what I do:

 <a href="{{ request.META.HTTP_REFERER }}">Referring Page</a>

Solution 3 - Django

This worked for me request.META.get('HTTP_REFERER') With this you won't get an error if doesn't exist, you will get None instead

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajeevView Question on Stackoverflow
Solution 1 - DjangoDaniel DiPaoloView Answer on Stackoverflow
Solution 2 - DjangoJeff BauerView Answer on Stackoverflow
Solution 3 - DjangoOscar Garcia - OOSSView Answer on Stackoverflow