[Django]-Issue with Django logout URL redirection

2👍

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 = '/'

0👍

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

0👍

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/’

Leave a comment