def logging_check(func):
def wrap(request, *args,**kwargs):
token = request.META.get('HTTP_AUTHORIZATION')
if not token:
result = {'code':403,'error:'please login')
return JsonResponse(result)
except Exception as e:
print('jwt decode error is %s'%e)
result = {'code':403,'error':'please login'}
return JsonResponse(result)
username = res['username']
user = User.object.get(username=username)
# request 传给了视图函数,最好起名有个性,比如myuser,如果是user可能会出莫名其妙的错误
# 在视图函数中直接request.myuser拿来用就可以了
request.myuser = user
return func(request, *args, **kwargs)
return wrap
method_decorator 可以将函数装饰器转换成方法装饰器。在某个视图类中的post或者put方法上挂上去用。