user.has_perm(perm) 中参数 perm 的格式问题 - Django

看一下官方文档中 has_perm(perm) 的说明:

has_perm(perm )
Returns  True   if the user has the specified permission, where perm is in the format  "<app   label>.<permission   codename> " . If the user is inactive, this method will always return  False .
(其实也可以使用 request.user.get_all_permissions() 在终端中参看该用户的权限值,顺便就可以看到 perm 的格式了。)

下面说明一下<app   label>.<permission   codename>  分别代表什么,以 app authenication 中 models.py 的定义为例:

# -*- coding: utf-8 -*-

from django.db import models

class Auth_control(models.Model):
    '''
    用于权限控制(测试)
    '''
    class Meta:
        permissions = (
            ('can_manage_users', '管理用户权限'),
        )


<app label> 的值为 authenication,
<permission codename> 的值为 can_manage_users,
所以参数 perm 的值就应该为 authentication.can_manage_users

然后就可以在 views.py 中做判断了:

if request.user.has_perm('authentication.can_manage_users'):
        can = True
    else:
        can = False


template  中:

{% if can %}  
            <a href="/newadmin/auth/">用户管理</a>
{% endif %}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值