[英]decorator() got an unexpected keyword argument
I have this error on Django view: 我在Django视图上有此错误:
TypeError at /web/host/1/
decorator() got an unexpected keyword argument 'host_id'
Request Method: GET
Request URL: http://127.0.0.1:8000/web/host/1/edit
Django Version: 1.10.4
Exception Type: TypeError
Exception Value:
decorator() got an unexpected keyword argument 'host_id'
and the urlpatterns is: urlpatterns是:
url(r'^host/(?P<host_id>[0-9]+)$', host, name='host'),
the view function is : 视图功能是:
@check_login
def host(request, host_id, *args, **kwargs):
h = Host()
# resultHost = h.get_host(host_id)
return render(request, 'web/host.html')
check_login below: check_login如下:
def check_login(f):
"""verify if user login"""
def decorator(request):
if request.session.get('user', None):
return f(request)
else:
return HttpResponseRedirect(reverse("web:login"))
return decorator
if I use the url without parameter "host_id" and the host function without host_id, the program will run perfect. 如果我使用不带参数“ host_id”的url和不带host_id的主机函数,则程序将运行完美。
so what's the problem? 所以有什么问题? Thank you. 谢谢。
The issue is in the check_login
decorator code. 问题出在check_login
装饰器代码中。 Specifically the problem is here: 具体来说,问题在这里:
def check_login(f):
"""verify if user login"""
def decorator(request): # <-- Only allows for a keyword value of 'request'
if request.session.get('user', None):
return f(request)
else:
return HttpResponseRedirect(reverse("web:login"))
return decorator
To resolve the issue you need to accept any extra keyword arguments that might be passed into the invoked decorator. 要解决此问题,您需要接受可能传递到调用的装饰器中的所有其他关键字参数。 You can do this by using a variadic argument which effectively says "Take any extra keyword arguments and represent them as a single value." 您可以通过使用可变参数来有效地做到这一点,可变参数可以有效地说明“采用任何额外的关键字参数并将它们表示为单个值。” By convention this single value ( **kwargs
in the example below) is the a dictionary where the keys are the names of the arguments and the values are the argument values. 按照惯例,此单个值(以下示例中为**kwargs
)是一个字典,其中的键是参数的名称,而值是参数的值。 The name kwargs
is a convention often used in Python for variadic arguments but is not mandatory - you can use any valid variable name. 名称kwargs
是Python中经常使用的惯例,用于可变参数,但不是强制性的-您可以使用任何有效的变量名。
def check_login(f):
"""verify if user login"""
def decorator(request, **kwargs): # <-- **kwargs will absorb any additional keyword arguments that are passed during invocation
if request.session.get('user', None):
return f(request, **kwargs)
else:
return HttpResponseRedirect(reverse("web:login"))
return decorator
Or to make it even more general you can accept both variadic positional and keyword arguments like so: 为了使它更加笼统,您可以接受可变参数的位置参数和关键字参数,如下所示:
def check_login(f):
"""verify if user login"""
def decorator(request, *args, **kwargs): # <-- *args will absorb any additional positional arguments
# <-- **kwargs will absorb any additional keyword arguments
if request.session.get('user', None):
return f(request, *args, **kwargs)
else:
return HttpResponseRedirect(reverse("web:login"))
return decorator
For more information regarding the use of the *args
and **kwargs
I'd recommend checking out the tutorial here: https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/ 有关使用*args
和**kwargs
更多信息,我建议您在此处查看该教程: https : //pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.