Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure where to redirect after a log out in Django?

Just wondering where I can set the url to redirect to after logout. I know you can set the login url. I want to redirect to my home page.

like image 927
DJ.MaSs Avatar asked Mar 15 '11 16:03

DJ.MaSs


2 Answers

Modern Django (2017+?) has a setting called LOGOUT_REDIRECT_URL.

Older Djangos / Original Answer

You don't need to overwrite or wrap anything.

According to the docs, you can just supply the next_page argument to the logout view. https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout

(r'^logout/$', 'django.contrib.auth.views.logout',
                          {'next_page': '/successfully_logged_out/'})
like image 131
Yuji 'Tomita' Tomita Avatar answered Oct 14 '22 10:10

Yuji 'Tomita' Tomita


One easier way:

Add 'next' parameter to your log-out request url. For example:

<a href="{% url 'auth_logout' %}?next=/path_to_the_page"> Logout</a>

Then the logout view will do the trick for you.

For after-login-redirect, you can just simply set it in settings.py:

LOGIN_REDIRECT_URL = '/path_to_the_page'
LOGIN_URL = '/path_to_the_page'
like image 70
YeRuizhi Avatar answered Oct 14 '22 12:10

YeRuizhi