django请求返回不同的类型图片json,xml,html的实例


Posted in Python onMay 22, 2018

django 返回数据的主要是用django.http.HttpResponse 中的HttpResponse 完成的

具体的我直接贴代码吧

from django.http import HttpResponse
### 引用
def index(request):
 if request.GET["type"] == "img":
  return HttpResponse(open("test.png","rb"),content_type="image/png")
  ## 这里 返回图片
 elif request.GET["type"] == "html":
  return HttpResponse(open("1.html","rb"),content_type="text/html")
  ## 返回 html文本
 elif request.GET["type"] == "xml":
  return HttpResponse(open("1.html","rb"),content_type="text/xml")
  ##返回 xml文本
 elif request.GET["type"] == "json":
  return HttpResponse({"code":"ok"},content_type="application/json")
  ##返回 json文本
 
from django.http import JsonResponse
def index(request):
 
 return JsonResponse({"code":"ok"})
 ## 这里的JsonResponse 更加简便的返回了json

以上这篇django请求返回不同的类型图片json,xml,html的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的dict字典结构操作方法学习笔记
May 07 Python
python matplotlib画图实例代码分享
Dec 27 Python
Python使用matplotlib绘制多个图形单独显示的方法示例
Mar 14 Python
Python 循环语句之 while,for语句详解
Apr 23 Python
完美解决Python 2.7不能正常使用pip install的问题
Jun 12 Python
python使用phoenixdb操作hbase的方法示例
Feb 28 Python
python实现飞机大战游戏
Oct 26 Python
Python函数和模块的使用总结
May 20 Python
python虚拟环境的安装和配置(virtualenv,virtualenvwrapper)
Aug 09 Python
如何通过python实现全排列
Feb 11 Python
使用python执行shell脚本 并动态传参 及subprocess的使用详解
Mar 06 Python
如何利用python读取micaps文件详解
Oct 18 Python
Django使用HttpResponse返回图片并显示的方法
May 22 #Python
python实现最长公共子序列
May 22 #Python
使用python存储网页上的图片实例
May 22 #Python
python3模块smtplib实现发送邮件功能
May 22 #Python
Python3.x爬虫下载网页图片的实例讲解
May 22 #Python
python3使用smtplib实现发送邮件功能
May 22 #Python
python抓取网站的图片并下载到本地的方法
May 22 #Python
You might like
echo, print, printf 和 sprintf 区别
2006/12/06 PHP
php5.2时间相差8小时
2007/01/15 PHP
php网站判断用户是否是手机访问的方法
2013/11/01 PHP
php时区转换转换函数
2014/01/07 PHP
ThinkPHP CURD方法之page方法详解
2014/06/18 PHP
PHP精确到毫秒秒杀倒计时实例详解
2019/03/14 PHP
js和html5实现手机端刮刮卡抽奖效果完美兼容android/IOS
2013/11/18 Javascript
JavaScript实现给按钮加上双重动作的方法
2015/08/14 Javascript
基于touch.js手势库+zepto.js插件开发图片查看器(滑动、缩放、双击缩放)
2016/11/17 Javascript
常用jQuery选择器汇总
2017/02/02 Javascript
JS获取浮动(float)元素的style.left值为空的快速解决办法
2017/02/19 Javascript
webpack独立打包和缓存处理详解
2017/04/03 Javascript
angular使用bootstrap方法手动启动的实例代码
2017/07/18 Javascript
vue Render中slots的使用的实例代码
2017/07/19 Javascript
JS实现简单表格排序操作示例
2017/10/07 Javascript
JS中FormData类实现文件上传
2020/03/27 Javascript
JavaScript实现沿五角星形线摆动的小圆实例详解
2020/07/28 Javascript
jQuery实现二级导航菜单的示例
2020/09/30 jQuery
[52:02]完美世界DOTA2联赛PWL S2 FTD.C vs SZ 第一场 11.27
2020/11/30 DOTA
Python open读写文件实现脚本
2008/09/06 Python
python读取html中指定元素生成excle文件示例
2014/04/03 Python
Python 检查数组元素是否存在类似PHP isset()方法
2014/10/14 Python
python uuid模块使用实例
2015/04/08 Python
python 3.6.2 安装配置方法图文教程
2018/09/18 Python
Python3 批量扫描端口的例子
2019/07/25 Python
利用Python如何制作贪吃蛇及AI版贪吃蛇详解
2020/08/24 Python
CSS3中Animation属性的使用详解
2015/08/06 HTML / CSS
什么是索引指示器
2012/08/20 面试题
设计部经理的岗位职责
2013/11/16 职场文书
机械设计专业应届生求职信
2013/11/21 职场文书
销售口号大全
2014/06/11 职场文书
公务员个人考察材料
2014/12/23 职场文书
讲座开场白台词和结束语
2015/05/29 职场文书
信仰观后感
2015/06/03 职场文书
关于国庆节的广播稿
2015/08/19 职场文书
SSM项目使用拦截器实现登录验证功能
2022/01/22 Java/Android