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
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.
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.
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? Use HTTP_REFERER value: for use in func return HttpResponseRedirect(request. META.
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'
Rajeev, this is what I do:
<a href="{{ request.META.HTTP_REFERER }}">Referring Page</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With