Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > Python网站web

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

来源:中文源码网    浏览:184 次    日期:2024-05-08 13:12:18
【下载文档:  django请求返回不同的类型图片json,xml,html的实例.txt 】


django请求返回不同的类型图片json,xml,html的实例
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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持中文源码网。

相关内容