[英]Why is my Django view decorator not getting the request passed to it?
I have a setup looking something like this: 我的设置看起来像这样:
def foo_decorator(function):
@wraps(function)
def decorator(*args, **kwargs):
print kwargs
return function(*args, **kwargs)
return decorator
@foo_decorator
def analytics(request, page_id, promotion_id):
pass
Outputting: 输出:
{'promotion_id': u'11','page_id': u'119766481432558'}
Why is my decorator not getting request
passed to it? 为什么我的装饰者没有收到
request
?
request
isn't a keyword argument to the view, it's the first positional argument. request
不是视图的关键字参数,它是第一个位置参数。 You can access it as args[0]
. 您可以将其作为
args[0]
访问。
def foo_decorator(function):
@wraps(function)
def decorator(*args, **kwargs):
print args[0]
return function(*args, **kwargs)
return decorator
I would recommend that you change the function signature to include request
explicitly: 我建议您更改功能签名以明确包含
request
:
def foo_decorator(function):
@wraps(function)
def decorator(request, *args, **kwargs):
print request
return function(request, *args, **kwargs)
return decorator
The request is not passed as a keyword argument. 请求不作为关键字参数传递。 It's in
args
, not kwargs
. 这是在
args
,而不是kwargs
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.