8

I would like to know if auth.logout clears session data or i have to do it by my self.

from django.contrib.auth.decorators import login_required
from django.contrib import auth
@login_required
def logout(request):
    auth.logout(request)
    return redirect('base:homepage')

Something like this...

from django.contrib.auth.decorators import login_required
from django.contrib import auth

@login_required
def logout(request):
    for sesskey in request.session.keys():
        del request.session[sesskey]
    auth.logout(request)
    return redirect('base:homepage')

Thanks!

CodeArtist
  • 5,534
  • 8
  • 40
  • 65
  • possible duplicate of [Does the django authentication logout function delete's session row in django\_session table?](http://stackoverflow.com/questions/12574459/does-the-django-authentication-logout-function-deletes-session-row-in-django-se) – karthikr Apr 05 '13 at 20:30

2 Answers2

20

Yes. Logout flushes the session.

This is its source:

def logout(request):
    """
    Removes the authenticated user's ID from the request and flushes their
    session data.
    """
    # Dispatch the signal before the user is logged out so the receivers have a
    # chance to find out *who* logged out.
    user = getattr(request, 'user', None)
    if hasattr(user, 'is_authenticated') and not user.is_authenticated():
        user = None
    user_logged_out.send(sender=user.__class__, request=request, user=user)

    request.session.flush()
    if hasattr(request, 'user'):
        from django.contrib.auth.models import AnonymousUser
        request.user = AnonymousUser()
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
0

If you mean deleting the record in 'django_session' table by clearing session data, I'm afraid logout function does not do that.

Usually, we have to clear expired session records in 'django_session' table by other ways. Like crontab task to run 'python manage.py clearsessions' periodically

See this for more information.

Jcyrss
  • 1,513
  • 3
  • 19
  • 31