如何从Django装饰器获取请求值

时间:2020-08-23 04:52:23

标签: django decorator

在我的装饰器中:

def check_user_identity(function):
    def wrap(request, *args, **kwargs):
        response_check_user = mock_response.check_user_identity(requested_data=request.data['msisdn'])
        if response_check_user['status'] == SUCCESS_CODE:
            return function(request, *args, **kwargs)
        return Response(data=INVALID_USER, status=status.HTTP_400_BAD_REQUEST)

    wrap.__doc__ = function.__doc__
    wrap.__name__ = function.__name__
    return wrap

在我看来: 在这里,request参数具有我请求继续执行的值。首先调用装饰器。我想从装饰器访问我的请求数据。

class VerifyWallet(APIView):
    @check_user_identity
    def post(self, request):
        logger.debug(msg=request.data)
        serializer = VerifyWalletSerializer(data=request.data)
        if not serializer.is_valid():
            logger.error(msg=serializer.errors)
            return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        return Response(data='success', status=status.HTTP_200_OK)

1 个答案:

答案 0 :(得分:0)

经过我所有的搜索,我找到了答案。值在 args[0] 中。