我正在为 Django View 创建一个装饰器,它将检查非 Django 管理的数据库中的权限。这是装饰器:
def check_ownership(failure_redirect_url='/', *args, **kwargs):
def _check_ownership(view):
def _wrapper(request, csi=None):
try:
opb_id=request.user.get_profile().opb_id
if opb_id and csi and model.is_users_server(opb_id, csi):
return view(*args, **kwargs)
except Exception, e:
logger.debug("Exception checking ownership: %s", str(e))
return HttpResponseRedirect(failure_redirect_url)
_wrapper.__dict__=view.__dict__
_wrapper.__doc__=view.__doc__
return _wrapper
return _check_ownership
这就是它的使用方式:
@check_ownership
def my_view(request, csi=None):
"""Process my request"""
正在调用 check_ownership() 并返回 _check_ownership()。当调用 _check_ownership() 时,它是通过 WSGIRequest 对象调用的,这正是我期望调用 _wrapper() 的对象。有人知道我的方法去了哪里以及如何找回它吗?我没有办法链接到下一个装饰器或实际 View 。
哦,CentOS 上的 Python 2.4.3 和 Django 1.1.1。
我想要恢复我的功能! ;)
谢谢。
tj
最佳答案
@check_ownership
def my_view(request, csi=None):
...
翻译成:
def my_view(request, csi=None):
...
my_view = check_ownership(my_view)
but check_ownership
不接受函数,但 _check_ownership
接受。这可能就是您的问题所在。
关于python - Django 装饰器获取 WSGIRequest 而不是预期的函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2291616/