response如何在python中使用

response如何在python中使用

今天就跟大家聊聊有关response如何在python中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Python主要用来做什么

Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。

下面简单记录下这三个response的区别

1、HttpResponse

它的返回格式为:HttpResponse(content=响应体, content_type=响应体数据类型, status=状态码)

1)它可以返回普通文本信息

HttpResponse("哈哈哈哈")

response如何在python中使用

2)它可以像文本一样追加内容:

res = HttpResponse("哈哈哈哈")

res.write("<p>恩,我们是一个测试段落</p>")

response如何在python中使用

3、它还可以返回图片,音频,视频等二进制文件信息

img = open(filepath,"rb")
data = img.read()
return HttpResponse(data, content_type="image/png")

response如何在python中使用

2、JsonResponse

它继承自HttpResponse,它主要用于返回json格式的数据

JsonResponse(jsonData,content_type="application/json")

response如何在python中使用

3、RestFramework框架封装的Response

它的返回格式为:

Response(data, status=None,template_name=None, header=None, content_type=None)

data:为python內建数据类型,DRF会使用render渲染器处理data

看完上述内容,你们对response如何在python中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。