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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

mzbqhbc12   2021-03-31 我要评论

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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们