Views.py
class templateList(PermissionRequiredMixin, TemplateView):
permission_required = 'accounts.template_all'
def get(self, request, *args, **kwargs):
#view logic
print(self.request.user.has_perms('accounts.template_all'))
return render(request, template_name, context)
account / models.py
class User(AbstractBaseUser, PermissionsMixin):
# some fields here
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
permissions = (
("template_all", "access to all templates"),
)
ViewName .___ mro____
(<class 'template.views.templateList'>, <class 'django.contrib.auth.mixins.PermissionRequiredMixin'>, <class 'django.contrib.auth.mixins.AccessMixin'>, <class 'django.views.generic.base.TemplateView'>, <class 'django.views.generic.base.TemplateResponseMixin'>, <class 'django.views.generic.base.ContextMixin'>, <class 'django.views.generic.base.View'>, <class 'object'>)
views.py中的
self.request.user.has_perms('accounts.template_all')
返回正确的布尔值,但是,self.has_permission()
每次都返回True。 permission_required
没有任何效果,即使打印返回false,用户仍然可以看到页面。 self.get_permission_required
alos返回正确的值。帮助表示赞赏。 最佳答案
简而言之:PermissionRequiredMixin
基类应该放在TemplateView
基类之前,这样MRO才是正确的,并且调度点指向PermissionRequiredMixin
的替代。PermissionRequiredMixin
修补dispatch(..)
方法(好吧,它添加了额外的检查,以查看用户是否具有适当的权限)。但是,在这里,您按顺序放置了子类,导致dispatch(..)
函数是View
类中的一个。
确实,如果我们看一下MRO,我们会看到:
>>> ViewName.__mro__
(<class 'ViewName'>, <class 'django.views.generic.base.TemplateView'>, <class 'django.views.generic.base.TemplateResponseMixin'>, <class 'django.views.generic.base.ContextMixin'>, <class 'django.views.generic.base.View'>, <class 'django.contrib.auth.mixins.PermissionRequiredMixin'>, <class 'django.contrib.auth.mixins.AccessMixin'>, <class 'object'>)
如果我们看一下调用
.dispatch(..)
时所调用的方法,我们会看到:>>> ViewName.dispatch
<function View.dispatch at 0x7f169e8f6620>
为了让mixin覆盖原始的
.dispatch(..)
函数,我们需要将其放在基类中,例如:# PermissionRequiredMixin is put before TemplateView
class ViewName(PermissionRequiredMixin, TemplateView):
permission_required = 'accounts.action_all'
# ...
然后,我们看到:
>>> ViewName.__mro__
(<class 'ViewName'>, <class 'django.contrib.auth.mixins.PermissionRequiredMixin'>, <class 'django.contrib.auth.mixins.AccessMixin'>, <class 'django.views.generic.base.TemplateView'>, <class 'django.views.generic.base.TemplateResponseMixin'>, <class 'django.views.generic.base.ContextMixin'>, <class 'django.views.generic.base.View'>, <class 'object'>)
>>> ViewName.dispatch
<function PermissionRequiredMixin.dispatch at 0x7f168b41d620>
关于python - django PermissionRequiredMixin权限不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54959548/