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'
and the urlpatterns is:
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:
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.
so what's the problem? Thank you.
The issue is in the
check_login
decorator code. Specifically the problem is here: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 namekwargs
is a convention often used in Python for variadic arguments but is not mandatory - you can use any valid variable name.Or to make it even more general you can accept both variadic positional and keyword arguments like so:
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/