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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With