django 中的 JsonResponse 和 HttpResponse 有什么区别

shr*_*tti 5 python django

可以说 result = {'a': 1, 'b': 2, 'c': 3}

这两者有区别吗:

return HttpResponse(json.dumps(a))
Run Code Online (Sandbox Code Playgroud)

return JsonResponse(a)
Run Code Online (Sandbox Code Playgroud)

Sla*_*lam 7

正如文档所述,主要区别在于

  • 自动序列化
  • 正确的内容类型
  • 默认情况下更安全的输入检查