Set Django Logout Redirect URL
Here's how to set up a URL to redirect to after logout in Django.
Make a URL Path
In one of your app's
urls.py files, create the urlpattern:
from django.contrib.auth import views as auth_views from django.urls import path from myproj import settings urlpatterns = [ path('logout/', auth_views.LogoutView.as_view(next_page=settings.LOGOUT_REDIRECT_URL), name='myapp_logout'), ]
Optionally create a setting
The above code uses a setting of
LOGOUT_REDIRECT_URL to store the location of the redirect. I got this pattern from looking at other docs and qna. But I don't think it's required. If you learn otherwise, please let me know.
Anyway, set it up in
LOGOUT_REDIRECT_URL = '/'
logout/url path is required
django.contrib.auth.LogoutView.as_viewis what you redirect to.
You do not need to pass a
myapp/logout.htmltemplate is required either
next_pagekwarg is required
LOGOUT_REDIRECT_URLby itself is insufficient
Try it out
python manage.py runserver xdg-open localhost:8000/logout
See that you are now redirected to