backend/django

django: how to know previous page in django

seul chan 2018. 3. 18. 05:00

Sometimes I need previous page for some reasons.

  • Redirect to previous page after delete object in detail page
  • Redirect to previous page when login/loggout …

You can know previous page with request.

Django request has many information with META tag. It has many useful data such as browser information, site domain, and so on.

In here, you can check example of request.META headers.

This META also has previous page information - HTTP_REFERER. If exist, it has it’s referring page.

So you can use like this

In views

prev_page = request.META.get('HTTP_REFERER')

In templates

{{ request.META.HTTP_REFERER }}