[django]django 3种返回json方法
django 3种返回json方法
1.手动组装字典返回
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from app01.models import Book
# Create your views here.
def get_book(request):
all_book = Book.objects.all()
d = []
for i in all_book:
d.append({'name': i.name})
return JsonResponse(d, safe=False)
2.JsonResponse返回
def get_book2(request):
from django.forms.models import model_to_dict
all_book = Book.objects.all()
d = []
for i in all_book:
d.append(model_to_dict(i)) # <-------针对一个对象()
return JsonResponse(d, safe=False) # 非字典要设置成false
一般自己的系统会从别的系统获取数据, 这里应该也仅限于展示, 所以JsonResponse还是有很多实用场景
def booapi(request):
from django.core.serializers import serialize
book_list = [
{'id': 1, 'name': 'ptyhon'},
{'id': 2, 'name': 'go'},
]
import json
return HttpResponse(json.dumps(book_list), content_type='application/json')
3.django自带的serializers返回
这个好像只能针对queryset操作,即本地db里的数据,不能操作从其他系统api获取到的list ,dict等
def get_book3(request):
from django.core.serializers import serialize
d = serialize('json', Book.objects.all()) # <-------针对一个queryset,[{}, {}]
# return HttpResponse(d)
return HttpResponse(d)
return render(request, 'myapp/index.html', {'foo': 'bar',}, content_type='application/xhtml+xml')
return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')
return HttpResponse(json.dumps(data), content_type='application/json', status=400)
JsonResponse = HttpResponse+content-type
model转dict方法
https://mp.weixin.qq.com/s/7gPLaCESHAB0dLgq7qZq5Q

使用类的__dict__方法
http://www.liujiangblog.com/course/django/171

[django]django 3种返回json方法的更多相关文章
- 转: .NET MVC3 几种返回 JSON 对象的方式和注意事项
.NET MVC3 几种返回 JSON 对象的方式和注意事项 转自:http://blog.csdn.net/xxj_jing/article/details/7382589 引言在用 .NET MV ...
- .NET中常用的几种解析JSON方法
一.基本概念 json是什么? JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. ...
- ajax 返回Json方法
public static void sendJsonData(String data) { ActionContext ac = ActionContext.getContext(); HttpSe ...
- Django 分页查询并返回jsons数据,中文乱码解决方法
Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...
- SpringMVC 返回JSON数据的配置
spring-mvc-config.xml(文件名称请视具体情况而定)配置文件: <!-- 启动Springmvc注解驱动 --> <mvc:annotation-driven> ...
- spring mvc 返回json的配置
转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Django中的 返回json对象的方式
在返回json对象的几种方式: 1 from django.shortcuts import render, HttpResponse # Create your views here. from d ...
- django 模板语法和三种返回方式
模板 for循环 {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} if语句 ...
- django 返回json
django返回json有以下三个版本 from django.http import HttpResponse import json from django.views import View f ...
随机推荐
- ConcurrentLinkedQueue since java1.5
1 父类 java.lang.Object 继承者 java.util.AbstractCollection<E> 继承者 java.util.AbstractQueue<E> ...
- Office Web Apps安装部署(二)
SharePoint 2013调用Office Web Apps 注意:调用OfficeWebApps的sharepoint应用的身份认证必须是基于声明的身份认证(claims-based authe ...
- 网络通信协议八之UDP协议详解
视频传输中使用UDP协议比较多 UDP协议的责任 >>创建进程到进程间的通信(由端口号完成) >>有限的差错控制,出现差错悄悄丢弃报文(注意这点和TCP协议的区别)
- 使用介质设备安装 AIX 以通过 HMC 安装分区
使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...
- ASP.NET异步
1.ASP.NET线程模型 在WEB程序中,天生就是多线程的,我们知道,一个WEB服务可以同时服务器多个用户,我们可以想象一下,WEB程序应该运行于多线程环境中,对于运行WEB程序的线程,我们可以称之 ...
- ==、===和Object.is()的区别
==.===和Object.is()的区别 一. 定义: ==:等同,比较运算符,两边值类型不同的时候,先进行类型转换,再比较: ===:恒等,严格比较运算符,不做类型转换,类型不同就是不等: Obj ...
- 深入理解无穷级数和的定义(the sum of the series)
Given an infinite sequence (a1, a2, a3, ...), a series is informally the form of adding all those te ...
- shell之使用paste命令按列拼接多个文件
试验文件: [root@db03 shell-script]# cat text1.txt 1 2 3 4 5 [root@db03 shell-script]# cat text2.txt orac ...
- sed中支持变量的处理方法
1.eval sed ’s/$a/$b/’ filename2.sed "s/$a/$b/" filename3.sed ’s/’$a’/’$b’/’ filename 4.sed ...
- Laravel 5.2 INSTALL- node's npm and ruby's bundler.
https://getcomposer.org/doc/00-intro.md Introduction# Composer is a tool for dependency management i ...
