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

DjangoAuthentication

Django Problem Overview


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.

Django Solutions


Solution 1 - Django

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

Solution 2 - Django

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'

Solution 3 - Django

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

Solution 4 - Django

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

LOGOUT_REDIRECT_URL = 'url name to redirect'

Solution 5 - Django

Redirect to current page

<a href="{% url 'logout' %}?next={{ request.path | urlencode }}">{% trans "Logout" %}</a>

Tested in Django 1.9.

See also: https://stackoverflow.com/questions/4591525/is-it-possible-to-pass-query-parameters-via-djangos-url-template-tag

Solution 6 - Django

You can even use named urls for your next parameter:

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

Solution 7 - Django

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

Solution 8 - Django

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

Solution 9 - Django

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.

Solution 10 - Django

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

Solution 11 - Django

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

Solution 12 - Django

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

ACCOUNT_LOGOUT_REDIRECT_URL = '/'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDJ.MaSsView Question on Stackoverflow
Solution 1 - DjangoYuji 'Tomita' TomitaView Answer on Stackoverflow
Solution 2 - DjangoYeRuizhiView Answer on Stackoverflow
Solution 3 - DjangoedelansView Answer on Stackoverflow
Solution 4 - DjangoSACHIN CHAVANView Answer on Stackoverflow
Solution 5 - DjangoCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 6 - DjangoblueFastView Answer on Stackoverflow
Solution 7 - Djangokesun421View Answer on Stackoverflow
Solution 8 - DjangoDonView Answer on Stackoverflow
Solution 9 - DjangoMendaView Answer on Stackoverflow
Solution 10 - DjangoDcode22View Answer on Stackoverflow
Solution 11 - DjangoEmad MohammadpoorView Answer on Stackoverflow
Solution 12 - DjangoArvind KushwahaView Answer on Stackoverflow