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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With