Search code examples
djangodjango-admindjango-authenticationdjango-allauth

Django: redirect admin logout to allauth login


I'm using the django-allauth login page to log users into my admin panel. This works fine, but when they logout from the admin panel, I want them to be sent directly back to the /accounts/login/ page (preferably to /accounts/login/?next=/admin/surveys/survey/), rather than to the /admin/logout/ page that says "thank you for spending quality time with the site." I tried the LOGOUT_URL property in my settings file, but it doesn't seem to do what I'm describing above. Here's some of my relevant code:

# urls.py

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

from mysite import views

from django.contrib import admin
admin.autodiscover()

from django.contrib.auth.decorators import login_required
admin.site.login = login_required(admin.site.login)

from django.contrib.auth import views as auth_views

urlpatterns = patterns('',
    url(r'^$', TemplateView.as_view(template_name='landingpage.html'), name='landingpage'),
    url(r'^surveys/', include('surveys.urls', namespace="surveys")),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^submitfeedback/$', views.submitfeedback, name='submitfeedback'),

    # tried this but it didn't work: url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/accounts/login/'}),
    # tried this but it didn't work: url(r'^admin/logout/$', 'django.contrib.auth.views.logout', {'next_page': '/accounts/login/'}),

    ## below for django-allauth
    url(r'^accounts/', include('allauth.urls')),
)

# settings.py

LOGIN_REDIRECT_URL = "/admin/surveys/survey/"
LOGOUT_URL = "/accounts/login/"

Solution

  • I think the easiest way to do this is not in views.py but in the template itself. What you want to do is copy the admin/base.html file (seen on Github here into your projects templates folder -- which you can then overwrite as needed. This template has all the header information that is inherited by the rest of the admin templates. Find the line that reads:

    <a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>
    

    and simply change that to:

    <a href="{% url 'account_logout' %}">{% trans 'Log out' %}</a>
    

    And it should now follow the format of the allauth logout on the rest of your site.