django项目封装Jsonresponse返回值的函数代码
restful.py文件内容如下:这样我们就可以用restful封装的函数来给客户端返回值,比如正常就是200,认证失败就是401,前端ajax拿到返回值可以根据返回值来判断是否正常
from django.http import JsonResponse class HttpCode(object): ok = 200 paramserror = 400 unauth = 401 methoderror = 405 servererror = 500 def result(code=HttpCode.ok, message="", data=None, kwargs=None): json_dict = {"code":code, "message":message,"data":data} if kwargs and isinstance(kwargs, dict) and kwargs.keys(): json_dict.update(kwargs) return JsonResponse(json_dict) def ok(): return result() def params_error(message="",data=None): return result(code=HttpCode.paramserror,message=message,data=data) def unauth(message="",data=None): return result(code=HttpCode.unauth,message=message,data=data) def method_error(message="",data=None): return result(code=HttpCode.methoderror,message=message,data=data) def server_error(message="",data=None): return result(code=HttpCode.servererror,message=message,data=data)
相关推荐
83510998 2020-07-18
thickbookszone 2020-06-14
xiaonamylove 2020-06-14
天高任鸟飞 2020-06-04
Magicsoftware 2020-06-01
Reiki 2020-05-04
小菜鸟的代码世界 2020-05-04
whynotgonow 2020-04-26
神龙 2020-02-10
ajaxtony 2020-02-03
HongKongPython 2020-01-30
GoatSucker 2020-01-24
liuyang000 2020-01-24
zhiyuan0 2020-01-06
狼窝 2019-12-12
yunfenglee 2019-12-05