7

Here is the problem I am facing with the Django Authenetication

  1. Access a page that requires a login.
  2. Logout (accessing django.contrib.auth.logout)
  3. Access the original login-protected page. You are still logged in

Any ideas how to solve the problem?

MY Django Session Settings are

SESSION_EXPIRE_AT_BROWSER_CLOSE = True SESSION_COOKIE_AGE = 3600

Thanks, Sujit

SystemMatrix
  • 71
  • 1
  • 2
  • 4
    When you say '3. Access the original ...' do you mean 'use the back arrow key and see the cached page image' or do you mean 'clicked on a link to a protected page and it worked when it shouldn't have'? – Peter Rowell Jan 15 '10 at 23:18
  • Both the cases ... When clicked back button & entering protected page url. – SystemMatrix Jan 18 '10 at 20:48
  • I added a new question, looks a lot like yours. Solved it yet! see http://stackoverflow.com/questions/14021913/django-logout-not-working – michel.iamit Dec 24 '12 at 13:15

5 Answers5

2

Basically, this should work:

from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    # Redirect to a success page.

Could you clarify by posting your view if it's not something like this?

jbochi
  • 28,816
  • 16
  • 73
  • 90
1

In Django 1.4.* I've had problems with the logout() function. It simply wasn't logging out my users.

Now I'm just using the contributed view to logout users and it works perfectly. Simply add this to your root urls.py file if you don't want to do anything else special:

(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/login'}),

and you'll be good to go.

Happy Djangoing.

Craig Labenz
  • 2,489
  • 3
  • 22
  • 17
0

views

from django.contrib.auth import logout

def logout_user(request):

"""
    logout the user
"""   

logout(request)
return HttpResponseRedirect('/qioness/connect/')

urls:

url(r'^userlogout/$',logout_user),

worked 4 me

okm
  • 23,575
  • 5
  • 83
  • 90
user1839132
  • 121
  • 2
  • 10
0

This worked for me. I was too stuck with this problem. Found the following solution on youtube.

My solution is a little modified though.

in views.py

from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect

def auth_logout(request):
  logout(request)
  return redirect('home')

in urls.py

url(r'^logout$', views.auth_logout, name='auth_logout'),  
saf1
  • 960
  • 3
  • 9
  • 21
0

Change session expire on close to False, with true it will not log you out until you close the browser

thenoob
  • 35
  • 1
  • 7