Django基础之JsonResponse对象

JsonResponse是HttpResponse的子类, 专门用来生成JSON编码的响应.

from django.http import JsonResponse

response = JsonResponse({"foo": "bar"})
print(response.content)

默认只能传递字典类型, 如果要传递非字典类型需要设置一下safe关键字参数.

response = JsonResponse([1, 2, 3], safe=False)
posted @ 2018-11-21 20:41  _杨魏  阅读(1511)  评论(0编辑  收藏  举报