Django_permissions,如何根据权限为菜单项创建模板? [英] Django_permissions, how to template my menu item based on the permissions?

查看:53
本文介绍了Django_permissions,如何根据权限为菜单项创建模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于给定的权限显示菜单项,例如,不是项目经理的用户,他们不能访问其他角色不同的用户的相同标题.我已经实现了Client仪表板和admin仪表板,以将用户与模板分开.

I want to display menu item based on my given permissions, for example users who are not a Project manager they cannot have access to the same header of other users with different roles. I have implemented Client dashboard and admin dashboard to separate the users from the templating.

这是我的代码:

class UsersListView(istView):
permission_required = 'can_view_user'
template_name = "user_list.html"
model = User

def dispatch(self, request, *args, **kwargs):
    if check_permission_BM_or_AM(request):
        if request.user.is_authenticated():
            return super(UsersListView, self).dispatch(request, *args, **kwargs)
    return redirect_to_login(self.request.get_full_path(),
                         self.get_login_url(),
                         self.get_redirect_field_name())

menuitem.html

<ul>
<li class="dropdown-submenu ">
    {% if can_view_user%}
        <a tabindex="-1" href="/user/list/"><span
                class="fa fa-fw fa-book "></span> Users</a>
    {% endif %}
</li>

如何根据给定的"permission_required ='can_view_user'权限使用模板使我的类视图正常工作?

how to use the template to get my class view to work based on the given `permission_required = 'can_view_user' permission?

提前谢谢

推荐答案

覆盖UsersListView的get_context_data方法,示例可以采用以下形式.

Override get_context_data method of your UsersListView, Sample can be of the form given below.

class UsersListView(ListView):
    # define your variables

    # dispatch method

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['can_view_user'] = self.can_view_user
        return context

这篇关于Django_permissions,如何根据权限为菜单项创建模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆