20

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.

lakshmen
  • 28,346
  • 66
  • 178
  • 276

5 Answers5

23

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
janos
  • 120,954
  • 29
  • 226
  • 236
18

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

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
14

Look at the source of the logout method, it should give you a clear idea what's going on. You can add extra arguments to the logout method to handle redirecting, but you can also append after the method for custom behaviour

def logout(request, next_page=None,
           template_name='registration/logged_out.html',
           redirect_field_name=REDIRECT_FIELD_NAME,
           current_app=None, extra_context=None):
    """
    Logs out the user and displays 'You are logged out' message.
    """
    auth_logout(request)
    redirect_to = request.REQUEST.get(redirect_field_name, '')
    if redirect_to:
        netloc = urlparse.urlparse(redirect_to)[1]
        # Security check -- don't allow redirection to a different host.
        if not (netloc and netloc != request.get_host()):
            return HttpResponseRedirect(redirect_to)
    #etc...
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
1
def logout(request):
    # Log out the user.
    logout(request)
    # Return to homepage.
    return HttpResponseRedirect(reverse('registeration:index'))
Zulu
  • 8,765
  • 9
  • 49
  • 56
Urvashi Meena
  • 37
  • 2
  • 9
1
urlpatterns =[
path('accounts/logout/', views.LogoutView.as_view(template_name="post_list.html"), name='logout'),
]

write a template_name as above inside it worked for me.Hope it might be useful. Thankyou

Nikhil Lingam
  • 121
  • 2
  • 12