解决Django响应JsonResponse返回json格式数据报错问题
作者:彭世瑜 时间:2022-12-07 00:06:02
代码
return JsonResponse({"name": "tom"})
报错:
TYPEERROR: In order to allow non-dict objects to be serialized
set the safe parmeter to False
解决:
return JsonResponse({"name": "tom"}, safe=False)
增加safe=false,使其接受列表
补充知识:python 里面 JsonResponse (book_list,safe=False)
代码为:
# 查询所有图书 、 增加图书
def get(self,request):
queryset = BookInfo.objects.all()
book_list = []
for book in queryset:
book_list.append({
'id':book.id,
'bread':book.bread
})
return JsonResponse (book_list,safe=False)
遇到问题:
JsonResponse (book_list,safe=False)
safe=False 这是什么鬼 ?
解决方案:
down 下源码后 :
def __init__(self, data, encoder=DjangoJSONEncoder, safe=True,
json_dumps_params=None, **kwargs):
if safe and not isinstance(data, dict):
raise TypeError(
'In order to allow non-dict objects to be serialized set the '
'safe parameter to False.'
)
if json_dumps_params is None:
json_dumps_params = {}
kwargs.setdefault('content_type', 'application/json')
data = json.dumps(data, cls=encoder, **json_dumps_params)
super(JsonResponse, self).__init__(content=data, **kwargs)
最终答案:
'In order to allow non-dict objects to be serialized set the ' 'safe parameter to False.'
来源:https://blog.csdn.net/mouday/article/details/82757183
标签:Django,JsonResponse,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python取余运算符知识点详解
2023-05-16 00:04:50
JS鼠标3次点击事件实现代码及扩展思路
2024-03-22 01:55:24
goland中使用leetcode插件实现
2023-07-23 10:43:09
PHP+redis实现添加处理投票的方法
2023-11-22 04:38:19
python使用turtle库与random库绘制雪花
2021-02-21 22:51:20
![](https://img.aspxhome.com/file/2023/4/72854_0s.jpg)
ASP 关于动态数据显示页面得锚点
2007-11-04 20:28:00
微信跳一跳小游戏python脚本
2023-07-06 10:15:15
![](https://img.aspxhome.com/file/2023/1/66811_0s.png)
Python Flask搭建yolov3目标检测系统详解流程
2023-07-07 12:40:05
![](https://img.aspxhome.com/file/2023/3/95203_0s.gif)
python中自带的三个装饰器的实现
2021-10-28 02:31:52
python抓取京东商城手机列表url实例代码
2022-11-11 18:23:04
Python解析、提取url关键字的实例详解
2023-07-08 11:52:27
![](https://img.aspxhome.com/file/2023/4/76994_0s.jpg)
uniqueidentifier转换成varchar数据类型的sql语句
2011-09-30 11:17:48
Go语言init函数详解
2024-05-11 09:18:31
![](https://img.aspxhome.com/file/2023/8/126368_0s.png)
HTML编写小经验
2011-06-14 09:43:14
详解ABP框架中Session功能的使用方法
2024-05-13 09:16:28
![](https://img.aspxhome.com/file/2023/2/126192_0s.png)
给网站界面预留退路
2009-03-25 20:32:00
![](https://img.aspxhome.com/file/UploadPic/20093/25/01-11s.jpg)
Python利用pynimate实现制作动态排序图
2023-05-25 03:31:32
![](https://img.aspxhome.com/file/2023/4/88994_0s.gif)
django 模版关闭转义方式
2023-07-18 15:23:03
![](https://img.aspxhome.com/file/2023/1/70201_0s.jpg)
Vue0.1的过滤代码如何添加到Vue2.0直接使用
2024-05-22 10:41:57
清除浮动的最简写法
2009-03-30 15:58:00