Django 中的JsonResponse 和 json区别

from django.http import JsonResponse
JsonResponse  里面代码会加这一个响应头 kwargs.setdefault('content_type', 'application/json') 告诉浏览器发送的是一个json字符串,不需要在进行 JSON.parse ;
return JsonResponse({"msg":"ok!"})  ----> 返回到ajax里面 这个直接就是一个对象了,不需要在进行 JSON.parse;

注意:
  JsonResponse 如果传递不是一个字典类型,这时候会报  In order to allow non-dict objects to be serialized set the ''safe parameter to False   
  ret = list(models.Comment.objects.filter(article_id=article_id).values("nid","content","parent_comment_id","user__username")) #传递是一个list 
  print(ret)
  return JsonResponse(ret,safe=False) #必须要加safe=False,不然会报'In order to allow non-dict objects to be serialized set the ''safe parameter to False.'


json 需要进行 JSON.parse

相关文章

django跟redis交互的示例:在django项目同名文件夹下的setti...
django应对sql注入攻击的方法:1.使用django自带的数据库API...
在django中引入jquery的方法:1.下载jquery源文件;2.新建st...
用django搭建网站的操作方法:1.快捷键win+r,打开运行窗口,...
如果 PyCharm 无法创建 Django 项目,可以尝试以下解决方法:...
要在PyCharm中安装Django框架,请按照以下步骤进行操作:1. ...