Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django logout redirects me to administration page

Tags:

django

logout

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?

like image 432
ducin Avatar asked Mar 18 '13 00:03

ducin


3 Answers

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.

like image 82
Gautham Nookala Avatar answered Oct 12 '22 02:10

Gautham Nookala


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.

like image 21
Menda Avatar answered Oct 12 '22 01:10

Menda


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/'})
like image 11
catherine Avatar answered Oct 12 '22 02:10

catherine