decorator()收到了意外的关键字参数 [英] decorator() got an unexpected keyword argument
问题描述
我在Django视图上遇到此错误:
I have this error on Django view:
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'
和urlpatterns是:
and the urlpatterns is:
url(r'^host/(?P<host_id>[0-9]+)$', host, name='host'),
查看功能是:
@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:
check_login below:
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
如果我使用不带参数"host_id"的url和不带host_id的宿主函数,则该程序将运行完美.
if I use the url without parameter "host_id" and the host function without host_id, the program will run perfect.
那是什么问题?谢谢.
推荐答案
问题出在 check_login
装饰代码中.具体的问题在这里:
The issue is in the check_login
decorator code. 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
要解决此问题,您需要接受可能传递到调用的装饰器中的所有其他关键字参数.您可以通过使用可变参数来有效地做到这一点,该参数可以有效地表示接受任何额外的关键字参数并将它们表示为单个值."按照约定,此单个值(在下面的示例中为 ** kwargs
)是一个字典,其中的键是参数的名称,而值是参数的值.名称 kwargs
是Python中常用的用于可变参数的约定,但不是强制性的-您可以使用任何有效的变量名.
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. The name kwargs
is a convention often used in Python for variadic arguments but is not mandatory - you can use any valid variable name.
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
或更广泛地说,您可以接受可变的 positional 和 keyword 自变量,如下所示:
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
有关使用 * args
和 ** kwargs
的更多信息,我建议在此处查看本教程:
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/
这篇关于decorator()收到了意外的关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!