如何添加'退出'功能到Django的管理站点

时间:2015-04-21 21:28:16

标签: python django

在我的Django项目中,我创建了一个单独的admin.py,在该文件中,我按如下方式扩展了AdminSite类:

class UserAdminSite(AdminSite):
    site_header = "Admin Site"
    site_title = "Manage User and Groups"
    login_form = AuthenticationForm

def has_permission(self, request):
    return request.user.is_active and request.user.is_admin_user

但后来我发现在该管理站点中,注销的左上角没有链接。如何将其添加到管理站点?

1 个答案:

答案 0 :(得分:0)

如果要扩展AdminSite,则应该已经具有注销功能

https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L349

@never_cache
def logout(self, request, extra_context=None):
    """
    Logs out the user for the given HttpRequest.
    This should *not* assume the user is already logged in.
    """
    from django.contrib.auth.views import logout
    defaults = {
        'current_app': self.name,
        'extra_context': dict(self.each_context(request), **(extra_context or {})),
    }
    if self.logout_template is not None:
        defaults['template_name'] = self.logout_template
    return logout(request, **defaults)

如果您查看管理站点的base.html,它正在寻找名称为admin的URL:logout

https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base.html#L46

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

您是否覆盖了默认的admin urls.py?您可以查看Django文档here,了解有关设置命名空间的详细信息。