网络编程
位置:首页>> 网络编程>> Python编程>> django请求返回不同的类型图片json,xml,html的实例

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

作者:mzbqhbc12  发布时间:2021-05-17 10:03:23 

标签: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

来源:https://blog.csdn.net/mzbqhbc12/article/details/54880181

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com