Re: Django back button issue after log-out

3,030 views
Skip to first unread message

Bill Freeman

unread,
Dec 17, 2012, 8:09:25 AM12/17/12
to django...@googlegroups.com


On Mon, Dec 17, 2012 at 5:14 AM, Ashish Sable <ashish...@gmail.com> wrote:

I have written simple registration(login,logout) Django apps.
 when i click on logout and then back button from browser
it shows me previous page. it should redirect me to login page. please help


 The back button in your browser just shows you cached stuff.  It doesn't contact the server, so there is nothing Django can do.

Ashish Sable

unread,
Dec 17, 2012, 9:07:49 AM12/17/12
to django...@googlegroups.com
yup... Got the solution just the bower cache needs to be cleared.

from django.views.decorators.cache import cache_control

@cache_control(no_cache=True, must_revalidate=True, no_store=True)
def view():

After logout it will render to login page.

Chris Cogdon

unread,
Dec 18, 2012, 12:33:03 AM12/18/12
to django...@googlegroups.com
But that means you're going to need to wrap _all_ your views in that structure, and that also means that none of those views will be cacheable.

That could be desirable, depending on your application.

You might also want to consider using @vary_on_cookie, which means that the cache will be specific to that user only for the current cookie values. a "logout" will clear the session cookies, and thus the cache is invalid.

Abhijeet Singh

unread,
Aug 12, 2016, 7:14:32 AM8/12/16
to Django users, ashish...@gmail.com
It still does not work. Still "Back" button takes it to last session page

James Schneider

unread,
Aug 14, 2016, 5:07:44 AM8/14/16
to django...@googlegroups.com, ashish...@gmail.com
On Thu, Aug 11, 2016 at 9:44 PM, Abhijeet Singh <abhije...@gmail.com> wrote:
It still does not work. Still "Back" button takes it to last session page

On Monday, December 17, 2012 at 7:37:49 PM UTC+5:30, Ashish Sable wrote:
yup... Got the solution just the bower cache needs to be cleared.

from django.views.decorators.cache import cache_control

@cache_control(no_cache=True, must_revalidate=True, no_store=True)
def view():

After logout it will render to login page.

On Monday, 17 December 2012 18:39:25 UTC+5:30, ke1g wrote:


On Mon, Dec 17, 2012 at 5:14 AM, Ashish Sable <ashish...@gmail.com> wrote:

I have written simple registration(login,logout) Django apps.
 when i click on logout and then back button from browser
it shows me previous page. it should redirect me to login page. please help



That's not typical behavior for a logout view for this exact reason. Normally a log out button will submit data to a server, and then the server will issue a redirect back to the main login page (rather than just rendering the page directly). Doing a redirect will, in almost all cases, cause the browser to bring up a dialog asking if the previous pages form data should be resubmitted. In most cases this is enough to keep users from accidentally hitting the back button with no permanent damage. 

However, keep in mind that like the previous poster mentioned, the local browser cache likely still contains copies (at least in part) of the previous pages that were visited, so it may be possible to extract the information from there. Not much that anyone can do about it, and is outside the control of Django.

Are you using the included login/logout views provided by Django in contrib.auth? They implement the correct behavior that prevents the back button from working by default.

Your views should also be implementing a check similar to the @login_required decorator, which should redirect the user back to the login page if they do not have a valid session.

-James

sidharth sharma

unread,
May 17, 2020, 9:21:09 AM5/17/20
to Django users
did you find the solution for this ?

Akshat Zala

unread,
May 19, 2020, 1:07:51 AM5/19/20
to Django users
In urls.py of the project directory:

path('login/', auth_views.LoginView.
as_view(template_name='users/login.html', redirect_authenticated_user=True),
name='login'),

On Monday, 17 December 2012 18:39:25 UTC+5:30, ke1g wrote:

Akshat Zala

unread,
May 22, 2020, 11:58:04 PM5/22/20
to Django users
Hey, 

Did the solution worked?
Reply all
Reply to author
Forward
0 new messages