Python全系列 教程
3567个小节阅读:5930.5k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
官网:https://docs.djangoproject.com/zh-hans/4.1/ref/request-response/#django.http.HttpResponse
返回给浏览器端的响应对象
xxxxxxxxxx
from django.http import HttpResponse
response = HttpResponse("Here's the text of the web page.")
response = HttpResponse("Text only, please.", content_type="text/plain")
response = HttpResponse(b'Bytestrings are also accepted.')
属性
HttpResponse.content:
表示响应的字符串
HttpResponse.charset:
表示响应将被编码的字符集,如果在HttpResponse实例化时没有给出,则会从中提取 content_type,如果没有设置content_type,则使用settings.DEFAULT_CHARSET
HttpResponse.status_code:
该响应的 HTTP 状态码
HttpResponse.reason_phrase:
响应的HTTP原因描述语,使用HTTP标准的默认原因描述语 除非明确设置,否则reason_phrase由 status_code 决定。
HttpResponse.streaming:
总是False,中间件通过此属性可以区分流式响应与常规响应
HttpResponse.closed:
如果response已经结束,则返回True,否则返回False
包含json格式内容的响应
xxxxxxxxxx
from django.http import JsonResponse
response = JsonResponse({'foo': 'bar'})
print(response.content) # 打印 b'{"foo": "bar"}'
实时效果反馈
1. 关于Django,响应对象说法错误的是?
A HttpResponse.content可以设置响应的内容
B HttpResponse.charset可以设置响应的编码
C JsonResponse.content可以设置响应的内容
D JsonResponse可以响应json结果
答案
1=>C