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: 响应的状态码