[Fixed]-Why is my Django view decorator not getting the request passed to it?

33👍

request isn’t a keyword argument to the view, it’s the first positional argument. You can access it as 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:

def foo_decorator(function):
    @wraps(function)
    def decorator(request, *args, **kwargs):
        print request
        return function(request, *args, **kwargs)

    return decorator

6👍

The request is not passed as a keyword argument. It’s in args, not kwargs.

Leave a comment