问题
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?
回答1:
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.
回答2:
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.
回答3:
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/'})
回答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/' %}
回答5:
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' ),
回答6:
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
.
回答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'
回答8:
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
回答9:
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/'
来源:https://stackoverflow.com/questions/15467831/django-logout-redirects-me-to-administration-page