[Django]-Django logout redirects me to administration page

74👍

If you are seeing the log out page of the Django administration site instead of your own log out page (your_application/templates/registration/logged_out.html), check the INSTALLED_APPS setting of your project and make sure that django.contrib.admin comes after your_application. Both templates are located in the same relative path and the Django template loader will use the first one it finds.

21👍

Tested on Django 1.6:

What I do is adding this into my urls.py:

(r'^management/logout/$', 'django.contrib.auth.views.logout'),

And then used it:

<a href="{% url "django.contrib.auth.views.logout" %}?next=/">Log out</a>

For the next argument, there you point to the right URL.

Tested on Django 2.1

Append to urlpatterns in urls.py:

from django.contrib.auth import views as auth_views

urlpatterns = [
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]

And then use it in the template:

<a href="{% url "logout" %}?next=/">logout</a>

More info can be found here.

13👍

The easiest solution is:

  1. Make sure your app comes before django.contrib.admin under installed apps in settings.py.

  2. Make sure your template is called logged_out.html.

12👍

According to docs, you can supply the next_page parameter to the logout view.

See LogoutView documentation.

(r'^logout/$', 'django.contrib.auth.views.logout',
    {'next_page': '/logged_out/'})

7👍

You can put LOGOUT_REDIRECT_URL in your settings.py file with a url name to redirect to, e.g. LOGOUT_REDIRECT_URL = 'index'

4👍

This is all fairly well explained in the manual, is there anything specific you don’t understand?

https://docs.djangoproject.com/en/dev/topics/auth/default/#how-to-log-a-user-out

from django.contrib.auth import logout

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

Alternatively if you don’t want to create your own view

https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout

{% url 'logout' next='/some/url/' %}

4👍

i was experiencing the same isssue following Django by example… found this url worked for me

url(r'^logout/$', 'django.contrib.auth.views.logout', { 'template_name': 'account/logout.html',}, name='logout' ),

4👍

Go to settings.py and add this code. “/” will redirect you to home

# Where to redirect during authentication
LOGIN_REDIRECT_URL = "/" #To go to home after login instead of getting redirected to accounts/profile on login which is default
LOGOUT_REDIRECT_URL = "/" #To logout back to the home page instead of the default admin logout page

2👍

I’m surprised no one has mentioned this, you can put this in your settings.py to redirect when logging in and logging out:

LOGIN_REDIRECT_URL = '/go-here-after-login/'
LOGOUT_REDIRECT_URL = '/go-here-after-logout/'

2👍

Summary of the most common solutions:

Make sure that your_app comes before django.contrib.admin in your INSTALLED_APPS list in your settings.py file.

Also make sure that your log out page is called ‘logged_out.html’ as pointed out in the answers above. Mine was called logout.html and didn’t work.

1👍

You can give the template to be rendered also in the href tag

{% if user.is_authenticated %}

  logged in as {{ user }}
  (<a href="{% url "logout" %}?next=myapp/templates/logoutmessage.html">logout</a>)

{% else %}
  ...
{% endif %}

if you use the default values in settings.py. Works for Django 3.1.

1👍

Make sure you added the following to your settings.py file:

LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'

It tells Django to redirect you to whatever page you want to go to after logging in or out.

0👍

Just replace loaders here, and auth templates will be found in “your_progect_apps/templates/registration”:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
        'loaders': [
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',
        ],
    },
},

]

Django v2.1

Leave a comment