對python中各個response的使用說明
Python django中我們經(jīng)常用的response有django中的 JsonResponse, HttpResponse,還有DRF中的Response
在使用的時候,經(jīng)常會不知道如何什么時候選擇用哪個response
下面簡單記錄下這三個response的區(qū)別
1、HttpResponse
它的返回格式為:HttpResponse(content=響應體, content_type=響應體數(shù)據(jù)類型, status=狀態(tài)碼)
1)它可以返回普通文本信息
HttpResponse('哈哈哈哈')

2)它可以像文本一樣追加內容:
res = HttpResponse('哈哈哈哈')
res.write('<p>恩,我們是一個測試段落</p>')

3、它還可以返回圖片,音頻,視頻等二進制文件信息
img = open(filepath,'rb')data = img.read()return HttpResponse(data, content_type='image/png')

2、JsonResponse
它繼承自HttpResponse,它主要用于返回json格式的數(shù)據(jù)
JsonResponse(jsonData,content_type='application/json')

3、RestFramework框架封裝的Response
它的返回格式為:
Response(data, status=None,template_name=None, header=None, content_type=None)
data:為python?冉ㄊ?堇嘈停?RF會使用render渲染器處理data
以上這篇對python中各個response的使用說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 解決Android studio xml界面無法預覽問題2. 什么是python的自省3. Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案4. Spring Boot和Thymeleaf整合結合JPA實現(xiàn)分頁效果(實例代碼)5. 詳解Android studio 動態(tài)fragment的用法6. Vuex localStorage的具體使用7. php模擬實現(xiàn)斗地主發(fā)牌8. IntelliJ IDEA安裝插件的方法步驟9. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現(xiàn)10. 使用Android studio查看Kotlin的字節(jié)碼教程

網(wǎng)公網(wǎng)安備