如何正确应用Django中的PermWrapper代理

时间:2014-05-02 11:43:57

标签: django python-2.7

我收到了错误消息:"' WSGIRequest'对象没有属性' has_module_perms'"在我的应用程序中使用PermWrapper进行RBAC控制时。

我追溯到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)

有没有人遇到过类似的问题?我在这里错过了什么?我的临时解决方法是在self.user.has_module_perms函数中将self.user.user.has_module_perms更改为__bool__

我正在使用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'
)

1 个答案:

答案 0 :(得分:2)

在我的settings.py中我也在TEMPLATE_CONTEXT_PROCESSORS中有这个:

"django.contrib.auth.context_processors.auth"