Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/"
like image 614
Gravity Grave Avatar asked Dec 19 '22 13:12

Gravity Grave


2 Answers

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.

like image 174
MBrizzle Avatar answered Dec 22 '22 12:12

MBrizzle


Add url(r'^admin/logout/$', 'yourappname.views.signout') in urls.py before including admin.site.urls like this:

urlpatterns = [
    url(r'^admin/logout/$', 'yourappname.views.signout'),
    url(r'^admin/', admin.site.urls),
    url(r'', include('appname.urls'))
]

Of course, in your app, you should define a signout view:

def signout(request):
    logout(request)
    return redirect(reverse('yourappname:index')) # or wherever you want
like image 38
spiky Avatar answered Dec 22 '22 11:12

spiky