'Issue with Django logout URL redirection

I have configured login and logout redirect setting in Django settings as below -

LOGOUT_REDIRECT_URL = '/login/'
LOGIN_REDIRECT_URL = '/locations/'

However, after logging out the redirection is done on the next= parameter, ie., the url is redirected to the same page from where logout is clicked.

For the reference, here is login/logout URL from my urls.py -

url(r'^', include('rest_framework.urls', namespace='rest_framework')),

PS: I am using Django 1.11 & DRF 3.7.3



Solution 1:[1]

The solution above didn't work for me with Django 2.1+

After looking into the logout function source code

from django.contrib.auth import logout

def logout_view(request):
  logout(request)
  # Redirect to a success page.

I wound up creating a custom logout view that calls the logout function and defines a redirect page

from django.contrib.auth import logout
from django.conf import settings
from django.shortcuts import redirect

def logout_view(request):
  logout(request)
  return redirect('%s?next=%s' % (settings.LOGOUT_URL, request.path))

Just call this view in your urls.py instead of "logout"

urlpatterns = [   
  path('accounts/logout/', views.logout_view),
]

And now a LOGOUT_URL redirect in settings.py will actually work

LOGOUT_URL = '/'

Solution 2:[2]

I ran into the same issue. The LOGOUT_REDIRECT_URL was not always working.

Solved it inside my myapp/urls.py:

from django.contrib.auth.views import logout

urlpatterns = [
  url(r'^logout/$', logout, {'next_page': '/login/'}, name='logout'),
]

Then in my desired template I inserted a link to: /'myapp'/logout/

You could as well define it in your main urls.py

Solution 3:[3]

you can add the below line in the settings.py to redirect to the login page if you're using Django in-built authentication.

LOGOUT_REDIRECT_URL ='/accounts/login/'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DTripodi
Solution 2 Santiago M. Quintero
Solution 3 Santosh Negalur