django.contrib.auth.logout in Django

You don't have to write a view for that, you can just do:

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

More info: https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout


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

Tags:

Django