0

I have a function based view that I only use to update a session:

def admin_privileges(request):
   // toggle request.session['is_admin']

   return

Typically I use this to toggle a session variable between True and False.

E.g. in a template:

<a href="{% url 'admin_privileges' %}">Toggle admin privileges</a> 

How can I pass a variable and amend return in admin_privileges to return the user to the original view it was requested from?

I don't want to use anything on the front end, and I can't use HTTP_REFERRER as it's not always set.

I thought of passing something via the URL from the referring view?

alias51
  • 8,178
  • 22
  • 94
  • 166

1 Answers1

0

There are 2 ways:

You can use request.META.get('HTTP_REFERER') in your view which will return previous page.

or (more safely, check this link to learn why)

You can pass the link (request.path for current page) as a parameter to admin_privileges link so:

<a href="{% url 'admin_privileges' %}?back={{ request.path }}">Toggle admin privileges</a> 

In the view, you can get that parameter using request.GET and redirect using Django's redirect utilities.

suayip uzulmez
  • 628
  • 8
  • 23