1. 程式人生 > >Django基礎之JsonResponse物件

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)