Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

request data not able to access with this code in wrapper function how can i python3 django decorator

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"})

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading