decorator() got an unexpected keyword argument

拜拜、爱过 提交于 2019-12-10 18:48:33

问题


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.


回答1:


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

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

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/



来源:https://stackoverflow.com/questions/41372523/decorator-got-an-unexpected-keyword-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!