def my_decorator(func):

def wrapper(request, *args, **kwargs):
print('===========')
return func(request, *args, **kwargs)
return wrapper

装饰某一请求方法

from django.utils.decorators import method_decorator
- 装饰某一个函数时导入模块

class DemoView(APIView):

permission_classes = ()

@method_decorator(my_decorator) # 仅仅装饰get请求
def get(self, request):
return HttpResponse('pppppppppppppppppppp')

def post(self, request):
return HttpResponse('aaaaa')

装饰所有请求方法

from django.utils.decorators import method_decorator
- 类装饰时导入模块

class DemoView(APIView):

permission_classes = ()

- 装饰类重写dispatch方法, (需要看源码)
@method_decorator(my_decorator)
def dispatch(self, request, *args, **kwargs):
- 调用父类方法
return super().dispatch(request, *args, **kwargs)

def get(self, request):

return HttpResponse('pppppppp')

def post(self, request):

return HttpResponse('aaaaa')

(一般这个)

import functools
def grained_permissions(func): -进行权利的细粒化
@functools.wraps(func)
def wrapper(self, request, args=None): - 参数是传入的函数有哪些入参,此处有哪些入参

if request.method == "POST":
return HttpResponse('xxxxx') -return 出去
else:
return func(self, request, args) - 返回某个函数,继续向下执行

#if False:
# return func(request,*args,**kwargs)
#else:
# return HttpResponse("您没有登陆")

return wrapper
class DemoView(APIView):

permission_classes = ()
@grained_permissions
def get(self, request):

return HttpResponse('pppppp')


def post(self, request):

return HttpResponse('aaaaa')