Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django request to find previous referrer

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  
like image 901
Rajeev Avatar asked Dec 10 '10 06:12

Rajeev


People also ask

How can I get previous URL in Django?

You can do that by using request. META['HTTP_REFERER'] , but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict . So be careful and make sure that you are using . get() notation instead.

What is request Meta get (' Http_referer ')?

get notation is to specify a default value of no value is present. E.g.: request. META. get("HTTP_REFERER", "localhost") would cause it to either return the actual value of HTTP_REFERER or return localhost if there is no HTTP_REFERER.

What is Http_referer Django?

django-http-referrer-policy provides a middleware class implementing the Referrer-Policy header for Django-powered sites.

How do I go back in Django template?

How do I go back in Django template? Use HTTP_REFERER value: for use in func return HttpResponseRedirect(request. META.


2 Answers

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' 
like image 56
Daniel DiPaolo Avatar answered Oct 02 '22 11:10

Daniel DiPaolo


Rajeev, this is what I do:

 <a href="{{ request.META.HTTP_REFERER }}">Referring Page</a> 
like image 26
Jeff Bauer Avatar answered Oct 02 '22 09:10

Jeff Bauer