django html显示xml,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","r
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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)