106

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
DJ.MaSs
  • 1,131
  • 3
  • 8
  • 11

12 Answers12

170

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/'})
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • 5
    This 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
70

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'
YeRuizhi
  • 953
  • 8
  • 12
42

Since Django 1.10, you can define a LOGOUT_REDIRECT_URL (see the docs)

edelans
  • 8,479
  • 4
  • 36
  • 45
23

You can redirect user anywhere by using LOGOUT_REDIRECT_URL in your setting.py file

LOGOUT_REDIRECT_URL = 'url name to redirect'
SACHIN CHAVAN
  • 415
  • 5
  • 16
14

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    Thank 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
7

You can even use named urls for your next parameter:

<a href="{% url 'auth_logout' %}?next={% url 'homepage' %}"> Logout</a>
blueFast
  • 41,341
  • 63
  • 198
  • 344
4

In your logout view, after you logout the user for good, return HttpResponseRedirect(url). Please see here for more details.

3

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.

Jeril
  • 7,858
  • 3
  • 52
  • 69
Menda
  • 1,783
  • 2
  • 14
  • 20
3

From docs you can write your own logout view (which can be just simple wrapper) overriding the 'next' page.

Don
  • 16,928
  • 12
  • 63
  • 101
0

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")

Dcode22
  • 97
  • 4
0

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

0

Add the below line in your project setting.py file:

ACCOUNT_LOGOUT_REDIRECT_URL = '/'
Arvind Kushwaha
  • 759
  • 2
  • 10
  • 18