Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error when adding an argument to kwargs

So I'm trying to make a decorator for my django view and I have it sort of working. The decorator looks like

# Decorator for the function - update(request, extra=None)
def check_super(view):
    def wrapper(request, *args, **kwargs):
        status = supercheck(request)

        if status=="USER":
            raise PermissionDenied
        else:
            args = [a for a in args]
            kwargs = dict((k, v) for k, v in kwargs.items())
            kwargs.update({'status':status})    # Offending line
            return view(request, *args, **kwargs)

    return wrapper

Basically I do a check with the supercheck function and I want to pass the result of that as another argument to my function but the line stated above results in the error

update() got an unexpected keyword argument 'status'

kwargs is just a dictionary, right? So I should be able to add new bindings to it like that but it keeps giving me that error. Doing it this way also results in the same errror

kwargs['status']=status
like image 741
Jared Joke Avatar asked Mar 24 '26 00:03

Jared Joke


1 Answers

I don't know why you're getting that -- I can't reproduce it on my machine.

That said, the easiest way to do what you want is to forget the mucking about with args and kwargs and just do

else:
    return view(request, *args, status=status, **kwargs)

assuming you know view takes an argument called 'status'. (If it doesn't, as John Spong said, well, there's your problem.)

Here's a bit more on *args, **kwargs, and ** in functions.

That said, it looks like all you want to do is check that the user passes your supercheck function. Check out the @user_passes_test decorator - it does exactly what you want.

like image 64
Christian Ternus Avatar answered Mar 26 '26 13:03

Christian Ternus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!