Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable browser 'Back' button after logout?

I am using python with django i want redirect users to login page when he clicks back button after logout. How to achieve this? where to write the code?

To test whether django admin handles this..i logged into django admin..logged out and then hit back button and i am able to see the previous page. Why django admin does not handle this.

This is the ccode for logout in django admin:

def logout(request):
  """
 Removes the authenticated user's ID from the request and flushes their
 session data.
 """
 request.session.flush()
 if hasattr(request, 'user'):
     from django.contrib.auth.models import AnonymousUser
     request.user = AnonymousUser()
like image 720
Vivek S Avatar asked Aug 03 '11 07:08

Vivek S


4 Answers

Finally found the solution:

from django.views.decorators.cache import cache_control

@cache_control(no_cache=True, must_revalidate=True)
def func()
  #some code
  return

This will force the browser to make request to server.

like image 160
Vivek S Avatar answered Oct 11 '22 05:10

Vivek S


You may find you need to use @cache_control(no_cache=True, must_revalidate=True, no_store=True) in chrome to fully stop any back button viewing.

The key thing being no_store for chrome as I found here 1

like image 45
Polygon Pusher Avatar answered Oct 11 '22 05:10

Polygon Pusher


+1 for Digital Cake's answer! This solved the problem of backing up into cached pages after logout on FireFox as well. I tried:

@cache_control(no_cache=True, must_revalidate=True)

on my views with no luck. Per Digital Cake, tried:

@cache_control(no_cache=True, must_revalidate=True, no_store=True)

and now Firefox backs up to the login screen.

like image 2
Jeff Wilson Avatar answered Oct 11 '22 06:10

Jeff Wilson


I know it's an old question, but the accepted answer did not work for me. i faced the same problem (using django 1.8 & Chrome)

Finally, I found the solution from the docs (django 1.7 or later). This will work for sure.

Just see the code below

from django.contrib.auth.decorators import login_required

@login_required(login_url='/login/')
def myview(request):
    return HttpResponse(render(request,'path_to_your_view.html'))

@login_required decorator is used to handle the issue. You can check more in doc

like image 2
Abhijeet Singh Avatar answered Oct 11 '22 07:10

Abhijeet Singh