django请求返回不同的类型图片json,xml,html的实例
django返回数据的主要是用django.http.HttpResponse中的HttpResponse完成的
具体的我直接贴代码吧
fromdjango.httpimportHttpResponse
###引用
defindex(request):
ifrequest.GET["type"]=="img":
returnHttpResponse(open("test.png","rb"),content_type="image/png")
##这里返回图片
elifrequest.GET["type"]=="html":
returnHttpResponse(open("1.html","rb"),content_type="text/html")
##返回html文本
elifrequest.GET["type"]=="xml":
returnHttpResponse(open("1.html","rb"),content_type="text/xml")
##返回xml文本
elifrequest.GET["type"]=="json":
returnHttpResponse({"code":"ok"},content_type="application/json")
##返回json文本
fromdjango.httpimportJsonResponse
defindex(request):
returnJsonResponse({"code":"ok"})
##这里的JsonResponse更加简便的返回了json
以上这篇django请求返回不同的类型图片json,xml,html的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短