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.
- [Django]-How to launch tests for django reusable app?
- [Django]-No Module named django.core
- [Django]-Django – how to detect test environment (check / determine if tests are being run)
13
The easiest solution is:
-
Make sure your app comes before
django.contrib.admin
under installed apps insettings.py
. -
Make sure your template is called
logged_out.html
.
- [Django]-Expire a view-cache in Django?
- [Django]-How to change field name in Django REST Framework
- [Django]-Django Rest Framework File Upload
12
According to docs, you can supply the next_page parameter to the logout view.
(r'^logout/$', 'django.contrib.auth.views.logout',
{'next_page': '/logged_out/'})
- [Django]-Django Rest Framework with ChoiceField
- [Django]-When to use Django get_absolute_url() method?
- [Django]-What's the best way to store a phone number in Django models?
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'
- [Django]-Django: get table name of a model in the model manager?
- [Django]-How to serve media files on Django production environment?
- [Django]-Are Django SECRET_KEY's per instance or per app?
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/' %}
- [Django]-How to resize the new uploaded images using PIL before saving?
- [Django]-Meaning of leading underscore in list of tuples used to define choice fields?
- [Django]-Django Aggregation: Summation of Multiplication of two fields
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' ),
- [Django]-How do I install an old version of Django on virtualenv?
- [Django]-Django request get parameters
- [Django]-How should I use DurationField in my model?
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
- [Django]-The number of GET/POST parameters exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS
- [Django]-Get javascript variable's value in Django url template tag
- [Django]-Django Admin – Disable the 'Add' action for a specific model
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/'
- [Django]-Altering one query parameter in a url (Django)
- [Django]-Django or Django Rest Framework
- [Django]-How to disable admin-style browsable interface of django-rest-framework?
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.
- [Django]-Django: Why do some model fields clash with each other?
- [Django]-How to set another Inline title in Django Admin?
- [Django]-How to add annotate data in django-rest-framework queryset responses?
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.
- [Django]-Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins
- [Django]-Requirements.txt greater than equal to and then less than?
- [Django]-How to manually assign imagefield in Django
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.
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
- [Django]-Testing email sending in Django
- [Django]-Django Query __isnull=True or = None
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
- [Django]-How to write setup.py to include a Git repository as a dependency
- [Django]-Access-Control-Allow-Origin in Django app
- [Django]-How to work around lack of support for foreign keys across databases in Django