tags:

views:

94

answers:

1

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.

+3  A: 

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.

Dejw
Oh... hahahaha... I'm an idiot >.< Thanks. I even had them lined up, one perfectly below the other, and it never occured to me.
Mark
Just wanted to mention--I always name my views with an _page at the end if they are used to display content. EG: Instead of logout, I'd write logout_page. It makes working with the Django APIs much, much easier.
b14ck
Mark: Don't worry. Almost everybody I know had that incident when they were starting in Django.
dannyroa
Yeahh.... I find Python to be a curious language that way. I always have to decide how much namespace I want to include... and this time it bit me :p
Mark