not able to access request data in deorator how can i do this ? in my wrapper function
def my_decorator(view_func):
def wrapper(request, *args, **kwargs):
print('-start-')
print(request.data)
print(args)
print(kwargs)
print('-now-')
response = view_func(request, *args, **kwargs)
return response
return wrapper
class HandymanCompleteProfileIos(APIView):
@my_decorator
def post(self, request, format = None):
return JsonResponse({"A":"a"})
The first argument of an instance method is self
, so the wrapper function should be defined with self
as the first argument, and pass it to the wrapped method when calling it:
def my_decorator(view_func):
def wrapper(self, request, *args, **kwargs):
print('-start-')
print(request.data)
print(args)
print(kwargs)
print('-now-')
response = view_func(self, request, *args, **kwargs)
return response
return wrapper
At the request of your comment, here is how you can parameterize the decorator with an additional argument extra_arg
:
def my_decorator(extra_arg):
def decorator(view_func):
def wrapper(self, request, *args, **kwargs):
print('-start-')
print(request.data)
print(args)
print(kwargs)
print(extra_arg)
print('-now-')
response = view_func(self, request, *args, **kwargs)
return response
return wrapper
return decorator