Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django logout infinite loop?

Tags:

django

Why is this running into an infinite loop, and 'TESTING' never gets printed? It seems like the logout function is causing the page to redirect to itself, but the documentation suggests that you can put your own redirect after calling that function.

def logout(request):
    logout(request)
    print 'TESTING'
    messages.success(request, 'Logged out.')
    return HttpResponseRedirect(request.GET['next'] or reverse('home'))

Is there another way to log the user out, if this logout function has some hidden redirect functionality?


Just took a peek at the source code, nothing suspicious there:

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()

I really can't imagine what's causing this.

like image 208
mpen Avatar asked Jul 12 '26 21:07

mpen


1 Answers

You have written:

def logout(request):
    logout(request)

which means You call the view itself - not the logout function.

You can fix this by writing:

from django.contrib import auth
# ...
def logout(request):
    auth.logout(request)
    # ...

In addition You do not have to write own logout view - it is given already by default as django.contrib.auth.views.logout.

like image 103
Dawid Avatar answered Jul 14 '26 14:07

Dawid