Django:将admin logout重定向到allauth login



我使用django-allauth登录页面将用户登录到我的管理面板。这工作得很好,但是当他们从管理面板注销时,我希望他们被直接发送回/accounts/login/页面(最好是/accounts/login/?next=/admin/surveys/survey/),而不是发送到/admin/logout/页面,上面写着"感谢您在网站上花费宝贵的时间。"我尝试了设置文件中的LOGOUT_URL属性,但它似乎没有做我上面描述的事情。以下是我的一些相关代码:

# 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/"

我认为最简单的方法不是在views.py中,而是在模板本身。你要做的是将admin/base.html文件(在Github上看到)复制到你的projects templates文件夹中——然后你可以根据需要覆盖它。此模板具有由其他管理模板继承的所有头信息。找到下面这行:

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

,然后改成:

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

现在它应该遵循您网站其余部分的allauth登出的格式。

在包含admin.site.urls之前将url(r'^admin/logout/$', 'yourappname.views.signout')添加到urls.py中,如下所示:

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

当然,在你的应用中,你应该定义一个签出视图:

def signout(request):
    logout(request)
    return redirect(reverse('yourappname:index')) # or wherever you want

相关内容

  • 没有找到相关文章

最新更新