45

I have provided a simple login functionality. For logout, I tried to use the built-in one. This is my urls.py:

(r'', include('django.contrib.auth.urls')),

And this is my template file:

{% if user.is_authenticated %}
logged in as {{ user }}
(<a href="{% url "logout" %}">logout</a>)
{% else %}

I have also enabled the default django admin site. When I click logout, it shows me the administration logout view. How can I pass the logout next page attribute to tell django which view to render?

Prav001
  • 343
  • 1
  • 5
  • 12
ducin
  • 25,621
  • 41
  • 157
  • 256

13 Answers13

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.

Gautham Nookala
  • 759
  • 5
  • 7
  • How is this answer answering " How can I pass the logout **next page attribute** to tell django which view to render?" – Louis May 21 '16 at 17:46
  • 14
    I didn't know that the logout template had to be named `logged_out.html`. Thanks a ton, you've put an end to almost a day of head-scratching :) – Soham Chowdhury Oct 09 '16 at 13:03
  • 2
    Wow. I did not find this in the documentation while reading about auth. Neat – karx Dec 19 '17 at 08:43
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.

Menda
  • 1,783
  • 2
  • 14
  • 20
  • im not sure why ppl didn't mark this as correct answer! this worked for me! – hpca01 Aug 27 '19 at 22:25
  • The simpliest, works like a charm. I recommend this if you want to logout without the need to create another template for logout view. – Shift 'n Tab Jul 23 '20 at 19:56
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.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
markh
  • 360
  • 4
  • 10
11

According to docs, you can supply the next_page parameter to the logout view. https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.views.logout

(r'^logout/$', 'django.contrib.auth.views.logout',
    {'next_page': '/logged_out/'})
catherine
  • 22,492
  • 12
  • 61
  • 85
6

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

Clemsang
  • 5,053
  • 3
  • 23
  • 41
John
  • 61
  • 2
  • 1
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/' %}
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • I thought I can use built-in django logout view and parametrize it somehow - this wouldn't force me to define my own custom view. – ducin Mar 18 '13 at 00:07
  • @tkoomzaaskz: that's also an option, it's explained here: https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views.logout – Wolph Mar 18 '13 at 00:12
  • 2
    Then I get `Reverse for 'logout' with arguments '()' and keyword arguments '{u'next': u'/some/url/'}' not found.`. I don't know what's wrong... The same error I get with the `login` from contrib... – ducin Mar 20 '13 at 23:10
  • Can you check which Django version you are running exactly? Also, how/where did you include your login urls? – Wolph Mar 21 '13 at 11:09
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' ),
Paddy Popeye
  • 1,634
  • 1
  • 16
  • 29
  • 1
    How is this answer answering " How can I pass the logout next page attribute to tell django which view to render?" – Louis May 21 '16 at 17:39
  • Thanks! This is the answer that worked for me. Specifying the template name fixed it so it no longer switched over to the admin logout. – excyberlabber Jun 08 '17 at 16:48
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
Samuel Folledo
  • 442
  • 6
  • 15
3

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.

  • correct, my issue is with order of the INSTALLED_APPS list at settings.py. – ZZA Sep 15 '22 at 10:13
  • but this also raise another problem because the admin panel logged out also redirect to frontend logout page – ZZA Sep 15 '22 at 10:15
  • Can confirm 'logged_out.html' was the issue for me as I had a slightly different format for the template name. – Olney1 Oct 12 '22 at 13:06
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/'
user500
  • 23
  • 3
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.

Lis
  • 555
  • 4
  • 26
Dr. Red
  • 141
  • 6
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.

ikonuk
  • 575
  • 8
  • 19
Bemigho
  • 11
  • 3
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