Files
cdtestplant_v1/utils/chen_response.py
2025-04-29 18:09:00 +08:00

26 lines
859 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import datetime
from django.http import HttpResponse
# 返回的时间有可能无法序列化,这里单独处理
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime("%Y-%m-%d")
else:
return json.JSONEncoder.default(self, obj)
## 重写django的HttpResponse注意返回的是axios里面的data!!!
class ChenResponse(HttpResponse):
def __init__(self, data=None, message='success', code=200, *args, **kwargs):
if data is None:
data = {}
std_data = {
"code": code,
"data": data,
"message": message,
"success": True
}
data = json.dumps(std_data, cls=DateEncoder)
super().__init__(data, *args, **kwargs)