Django: getting previous url

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.

# Returns None if user came from another website
request.META.get('HTTP_REFERER')

Note: I gave this answer when Django 1.10 was an actual release. I'm not working with Django anymore, so I can't tell if this applies to Django 2


You can get the referring URL by using request.META.HTTP_REFERER

More info here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META


I can't answer @tryingtolearn comment, but for future people, you can use request.META['HTTP_REFERER']

Tags:

Python

Url

Django