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.
12 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/'})

- 115,817
- 29
- 282
- 245
-
5This does not work if you have done a generic import of all auth urls. Easier to use @YeRuizhi's answer below – RunLoop Jun 10 '15 at 06:46
-
@RunLoop I approve of this other method. Only problem is you have to remember to use it every single time you build said link. So if this works in a standard build, it's more reliable. If not, then the next easiest solution makes sense. – Yuji 'Tomita' Tomita Jun 10 '15 at 07:00
-
i can't undersand how to reverse this url from view. – meteor Oct 23 '15 at 05:54
-
This is an awesome solution. I am pushing it right back to login page. Is there a way to add a system message through this line as well? so as to create a popup? "You have successfully logged out" – arcee123 Oct 23 '15 at 23:58
-
The forward slash at the start of 'next page' URL : '/successfully_logged_out/' is vital, without it, Django attempts to concatenate the "logout" URL with the "successfully_logged_out" URL, this creates a new 'illegal' URL. Makes life a tad difficult if you want to present Users with your Login Page after they logout. – Rickka Jun 07 '16 at 13:09
-
@Rickka indeed, it's a standard HttpResponseRedirect(next_page), so if you supply a relative URL, it will do a relative redirect. – Yuji 'Tomita' Tomita Jun 09 '16 at 01:33
-
Since Django 1.10, there is a better way : a setting for that, see this answer https://stackoverflow.com/a/42615214/1570104 – edelans Mar 15 '18 at 12:52
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'

- 953
- 8
- 12
-
39
-
6Coming soon... https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-LOGOUT_REDIRECT_URL – Carl Apr 14 '16 at 20:10
-
21
-
Good answer. Sticking this in my `base.html` and forgetting about it. :D (I'm using vintage Django on Debian Jessie :S so the new settings aren't available.) – underscore_d Jul 14 '17 at 18:13
You can redirect user anywhere by using LOGOUT_REDIRECT_URL in your setting.py file
LOGOUT_REDIRECT_URL = 'url name to redirect'

- 415
- 5
- 16
Redirect to current page
<a href="{% url 'logout' %}?next={{ request.path | urlencode }}">{% trans "Logout" %}</a>
Tested in Django 1.9.
See also: Is it possible to pass query parameters via Django's {% url %} template tag?

- 347,512
- 102
- 1,199
- 985
-
1Thank you, this helped me in case, when LOGOUT_REDIRECT_URL didn't (coz I have different paths in testing and production environment). – Dmytro Gierman Sep 06 '19 at 06:48
-
On the other hand can use {{ request.get_full_path }} to get the full url (including the trailing query string) as well. Example: "/music/bands/the_beatles/?print=true" – Athibet Prawane Jul 22 '22 at 08:03
You can even use named urls for your next parameter:
<a href="{% url 'auth_logout' %}?next={% url 'homepage' %}"> Logout</a>

- 41,341
- 63
- 198
- 344
In your logout view, after you logout the user for good, return HttpResponseRedirect(url). Please see here for more details.
If you want to set the redirection URL on client level, you can do it in the urls.py
:
(r'^management/logout/$', 'django.contrib.auth.views.logout'),
And then in the template:
<a href="{% url 'django.contrib.auth.views.logout' %}?next=/">
Log out
</a>
Where the next
, you point to the right URL.
If you have defined your own urls (and not imported generic auth urls) and are using the standard django auth views, them you can simply add (template_name='example.html') in the path.
path('logout/',auth_views.LogoutView.as_view(template_name='homepage.html'),name="logout")

- 97
- 4
add this in you project setting.py file
LOGOUT_REDIRECT_URL = '/'
you can write your URL between '' I use my index page for logout default redirect
Add the below line in your project setting.py file:
ACCOUNT_LOGOUT_REDIRECT_URL = '/'

- 759
- 2
- 10
- 18