通过django用户权限限制管理菜单项

时间:2018-05-19 14:23:04

标签: python django django-admin wagtail

我正在努力扩展w ,,我正在使用下一个钩子: python @hooks.register('register_settings_menu_item') def register_django_admin_menu_item(): return MenuItem(_('Site Admin'), '/django-admin', classnames='icon icon-wagtail-inverse', order=20000)

我获得了django管理页面的链接,但我想将该项目仅限于具有员工权限的用户。
enter image description here

我该怎么做?

2 个答案:

答案 0 :(得分:4)

试试这个:

    class CustomAdminMenuItem(MenuItem):
        def is_shown(self, request):
            return request.user.is_staff


    @hooks.register('register_admin_menu_item')
    def register_custom_admin_menu_item():
        return CustomAdminMenuItem(_('Site Admin'), reverse('admin_menu'), classnames='icon icon-folder-inverse', order=20000)

答案 1 :(得分:-1)

有一个装饰者:

from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
@hooks.register('register_settings_menu_item')
def register_django_admin_menu_item():
    return MenuItem(_('Site Admin'), '/django-admin', classnames='icon icon-wagtail-inverse', order=20000)