107.JsonResponse
JsonResponse类:
用来dump字符串成json字符串,然后返回将json字符串封装成Response对象返回给浏览器,并且它的Content-Type是application/json。示例代码如下:
from django.http import HttpResponse, JsonResponse
def json_view(request):
# 默认情况下,JsonResponse只能对字典进行dump,如果想要对非字典的数据进行dump,那么就需要JsonResponse传递一个safe=False参数
person = {
'username': '孤烟逐云',
'age': 18,
'height': 160,
}
# 1.第一种方式
person_json = json.dump(person)
<!--在将json对象包装成一个HttpResponse对象的时候,注意,一定要指定HttpResponse的的请求头的类型,不指定的话,默认为text/html。此时要指定为json: content_type='application/json'-->
response = HttpResponse(person_json, content_type='application/json')
# 2.第一种方式可以直接使用JsonResponse实现
response = JsonResponse(person)
return response
# 3. 将非字典的字符串转换成json字符串
<!--对非字典的数据进行dump,应该在使用HttpResponse的时候,传入一个safe=False参数,示例代码如下:-->
person = '孤烟逐云'
reponse1 = JsonResponse(person, safe=False)
return response1
107.JsonResponse的更多相关文章
- Entity Framework 6 Recipes 2nd Edition(10-7)译 -> TPH继承模型中使用存储过程
10-7. TPH继承模型中使用存储过程 问题 用一个存储过程来填充TPH继承模型的实体 解决方案 假设已有如Figure 10-7所示模型. 我们有两个派生实体: Instructor(教员)和St ...
- 常用的107条Javascript
1. document.write( " "); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document- >html- >( ...
- [nginx] connect() failed (111: Connection refused) while connecting to upstream, client: 101.18.123.107, server: localhost,
nginx一直报错, 2016/12/02 10:23:19 [error] 1472#0: *31 connect() failed (111: Connection refused)while c ...
- 新手107条常用javascript语句
1.document.write( " "); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document- >html- >(head,body)4 ...
- 找规律 SGU 107 987654321 problem
题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=107 /* 题意:n位数的平方的后面几位为987654321的个数 尼玛,我看描述这 ...
- js-分享107个js中的非常实用的小技巧(借鉴保存)
转载原文:http://***/Show.aspx?id=285 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:doc ...
- python解无忧公主数学题107.py
python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...
- leetcode 107
107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order trav ...
- SGU 107
107. 987654321 problem time limit per test: 0.25 sec. memory limit per test: 4096 KB For given numbe ...
随机推荐
- 指令——pwd
完整的指令的标准格式:Linux通用的格式 #指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个. 指令pwd: 用法:#pwd(print workin ...
- 了解facade设计模式
Facade模式 Facade模式要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行.Facade模式提供一个高层次的接口,使得子系统更易于使用. 就如同医院的接待员一样,Fac ...
- 自定义jqGrid编辑功能,当行获取焦点时编辑,失去焦点时保存
http://www.360doc.com/content/17/0719/15/9200790_672577533.shtml /********************************** ...
- [NOIP2017] T4 跳房子 DP+二分
Description 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 n 个格子,这些格子都在同一条直线 ...
- 实验吧-密码学-try them all(加salt的密码)、robomunication(摩斯电码)、The Flash-14(闪电侠14集)
try them all(加salt的密码) 首先,要了解什么事加salt的密码. 加salt是一种密码安全保护措施,就是你输入密码,系统随机生成一个salt值,然后对密码+salt进行哈希散列得到加 ...
- MongoDB 教程
版权所有,未经许可,禁止转载 章节 MongoDB 入门 MongoDB 优势 MongoDB 安装 MongoDB 数据建模 MongoDB 创建数据库 MongoDB 删除数据库 MongoDB ...
- js实现鼠标单击或者双击事件
// timer为全局变量 getClickEmail1(_type) { clearTimeout(this.timer); if (_type == 1) { if (event.detail = ...
- 四、CI框架之通过URL路径访问C中的函数
一.在C中写一个test001函数 二.在路径http://127.0.0.1/CodeIgniter-3.1.10/index.php/welcome/test001中访问 不忘初心,如果您认为这篇 ...
- 学术Essay写作中Introduction的正确打开方式
其实在学术essay写作过程中,很多留学生经常不知道如何写introduction,所以有些开头的模板句就出现了,比如,With the development of society/With the ...
- (递归)P1025 数的划分
题解: #include<iostream>using namespace std;int ret=0,m_n;void p(int n,double k,int j){ if(k==1) ...