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

yipeiwu_com6年前Python基础

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实现多进程的四种方式

方式一: os.fork() # -*- coding:utf-8 -*- """ pid=os.fork() 1.只用在Unix系统中有效,Windows系统中无效 2.f...

Python中用Ctrl+C终止多线程程序的问题解决

复制代码 代码如下:#!/bin/env python # -*- coding: utf-8 -*- #filename: peartest.py imp...

python绘制多个子图的实例

python绘制多个子图的实例

绘制八个子图 import matplotlib.pyplot as plt fig = plt.figure() shape=['.','o','v','>','<...

对pandas将dataframe中某列按照条件赋值的实例讲解

在数据处理过程中,经常会出现对某列批量做某些操作,比如dataframe df要对列名为“values”做大于等于30设置为1,小于30设置为0操作,可以这样使用dataframe的ap...

Python绘制二维曲线的日常应用详解

Python绘制二维曲线的日常应用详解

使用Python绘制出类似Excel或者MATLAB的曲线还是比较容易就能够实现的,需要用到的额外库有两个,numpy和matplotlib。使用这两个模块实现的曲线绘制其实在一定程度上...