脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - django请求返回不同的类型图片json,xml,html的实例

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

2021-02-23 00:21mzbqhbc12 Python

今天小编就为大家分享一篇django请求返回不同的类型图片json,xml,html的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

具体的我直接贴代码吧

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/mzbqhbc12/article/details/54880181

延伸 · 阅读

精彩推荐