156👍
✅
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'
26👍
Rajeev, this is what I do:
<a href="{{ request.META.HTTP_REFERER }}">Referring Page</a>
- [Django]-Do I need Nginx with Gunicorn if I am not serving any static content?
- [Django]-How can I get all the request headers in Django?
- [Django]-Not able to create super user with Django manage.py
11👍
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
- [Django]-Django models: get list of id
- [Django]-How to use pdb.set_trace() in a Django unittest?
- [Django]-How about having a SingletonModel in Django?
2👍
With 2 lines of code below, I could get referer in overridden get_queryset()
in Django Admin:
# "store/admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
def get_queryset(self, request):
print(request.META.get('HTTP_REFERER')) # Here
print(request.headers['Referer']) # Here
return super().get_queryset(request)
Output on console:
http://localhost:8000/admin/store/person/ # request.headers['Referer']
http://localhost:8000/admin/store/person/ # request.META.get('HTTP_REFERER')
- [Django]-Django Celery Logging Best Practice
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
- [Django]-Changing a project name in django
Source:stackexchange.com