2 优化返回值+class封装

class封装返回值

常见的我们返回值都是{"xx": "xx", },其实我们可以优化返回值,通过class封装。

from django.http import JsonResponse

class Response(object):
    def __init__(self):
        self.status = False
        self.detail = None
        self.data = None

    @property
    def dict(self):
        return self.__dict__

def sms_send(request):
    res = Response()
    ...
    if False:
        res.detail = {"detail": {"mobile": ["发送失败,请稍后重试", ]}}
        return JsonResponse(res.dict)

    res.status = True
    return JsonResponse(res.dict)
posted @ 2022-07-26 17:58  角角边  Views(25)  Comments(0)    收藏  举报