无法在Django视图中调用装饰器内部函数

时间:2015-06-22 07:25:29

标签: python django

我今天面临一个问题,这是我的基于Django类的视图和装饰。

def my_decorator(view_func):
    def _decorator(request, *args, **kwargs):
        print "I am here"
        # maybe do something before the view_func call
        response = view_func(request, *args, **kwargs)
        # maybe do something after the view_func call
        return response
    return wraps(view_func)(_decorator)

class ConfigletList(APIView):
    """
    List all Configlets, or create a new Configlet.
    """
    @my_decorator
    def get(self, request, format=None):
        configlets = Configlet.objects.filter(status = True).order_by('name')
        serializer = ConfigletSerializer(configlets, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = ConfigletSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

从get请求我调用一个装饰器来访问_decorator函数中的请求对象。但它没有被召集。请告诉我这段代码中的问题。我之前从未在装饰器中遇到任何此类问题。

0 个答案:

没有答案