1. 程式人生 > >Django基礎之response物件

Django基礎之response物件

與由DJango自動建立的HttpRequest物件相比, HttpResponse物件是我們的職責範圍了. 我們寫的每個檢視都需要例項化, 填充和返回一個HttpResponse.
HttpResponse類位於django.http模組中.

1. 使用

傳遞字串

from django.http import HttpResponse
response = HttpResponse("Here's the text of the Web page")
response = HttpResponse("Test only, please.", content_type="text/plain")

設定或刪除響應頭資訊

response = HttpResponse()
response["COntent-Type"] = "text/html; charset=UTF-8"
def response["Content-Type"]

2. 屬性

HttpResponse.content: 響應內容
HttpResponse.charset: 響應內容的編碼
HttpResponse.status_code: 響應的狀態碼