【发布时间】:2014-12-28 13:54:43
【问题描述】:
在我的应用程序中将 PermWrapper 用于 RBAC 控件时,我收到错误消息:“'WSGIRequest' object has no attribute 'has_module_perms'”。
我追踪到django.contrib.auth.context_processors.py,发现有时传入的user参数是一个完整的WSGIRequest对象,而不是WSGIRequest对象中的用户对象。
class PermWrapper(object):
def __init__(self, user):
self.user = user
所以稍后当它运行到 PermLookupDict 类的 bool 函数时,self.user 是一个 WSGIRequest 并且不会调用“has_module_perms”函数,因此会引发异常。
class PermLookupDict(object):
def __bool__(self):
return self.user.has_module_perms(self.module_name)
有没有人遇到过类似的问题?我在这里想念什么?我的临时解决方法是在__bool__ 函数中将self.user.has_module_perms 更改为self.user.user.has_module_perms。
我使用的是 Django 1.5,我的设置文件如下所示:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
'a10ext.context_processors.a10ext',
'django.contrib.auth.context_processors.PermWrapper'
)
【问题讨论】:
标签: django python-2.7