Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.contrib.auth.logout in Django

Tags:

django

I would like to use the logout function from Django but not sure how to use it properly.I have been referring to this Django User Authenication: https://docs.djangoproject.com/en/dev/topics/auth/ and it reads

from django.contrib.auth import logout

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

The confusing part for me is the # Redirect to a success page. How do i redirect it to another page. Should I use HttpResponseRedirect or add additional arguments to logout(request). I am not sure what to do.. Need some guidance.

like image 435
lakshmen Avatar asked Jun 28 '12 08:06

lakshmen


1 Answers

Django has a shortcut method called redirect. You could use that to redirect like this:

from django.contrib.auth import logout
from django.shortcuts import redirect

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

Where home is the name of a url pattern you defined in urls.py like this:

urlpatterns = patterns('',
    url(r'^$', 'blah.views.index', name='home'))
)

In the redirect call you could use a path as well, like / to redirect to the site root, but using named views is much cleaner.

PS: the code posted by @Hedde is from django.contrib.auth.views module, logout method. If that's what you want to use, you can import it like this:

from django.contrib.auth.views import logout
like image 112
janos Avatar answered Oct 14 '22 06:10

janos