基于 Django1.10 文档的深入学习(14)—— Request and response objects 之 HttpResponse objects
来源:互联网 发布:手机淘宝一键秒杀在哪? 编辑:程序博客网 时间:2024/06/05 00:24
HttpResponse objects
class HttpResponse[source]
与由Django自动创建的HttpRequest对象相反,HttpResponse对象是您的责任。 您编写的每个视图都负责实例化,填充和返回HttpResponse。
HttpResponse
类住在django.http
模块中。
用法
传递字符串
典型的用法是将页面的内容作为字符串传递给HttpResponse
构造函数:
>>> 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()>>> response.write("<p>Here's the text of the Web page.</p>")>>> response.write("<p>Here's another paragraph.</p>")
传递迭代器
最后,您可以将HttpResponse传递给迭代器而不是字符串。 HttpResponse将立即使用迭代器,将其内容存储为字符串,并将其丢弃。 具有close()方法(如文件和生成器)的对象将立即关闭。
如果您需要将响应从迭代器流式传输到客户端,则必须使用StreamingHttpResponse类。
0 0
- 基于 Django1.10 文档的深入学习(14)—— Request and response objects 之 HttpResponse objects
- 基于 Django1.10 文档的深入学习(13)—— Making queries 之 Q objects
- Request and Response objects
- 基于 Django1.10 文档的深入学习(17)——Customizing authentication 之 Custom users and permissions
- 基于 Django1.10 文档的深入学习(24)—— Form and field validation 之 cleaned_data
- 基于 Django1.10 文档的深入学习(2)—— Settings.py 之 STATIC_*
- 基于 Django1.10 文档的深入学习(3)—— models.py 之 FileField
- 基于 Django1.10 文档的深入学习(4)—— models.py 之 class Meta
- 基于 Django1.10 文档的深入学习(5)—— Making queries 之 FOO_set
- 基于 Django1.10 文档的深入学习(6)—— Translation 之 short_description
- 基于 Django1.10 文档的深入学习(8)—— Model field reference 之 choices
- 基于 Django1.10 文档的深入学习(9)—— Extra instance methods 之 get_FOO_display()
- 基于 Django1.10 文档的深入学习(11)—— django.shortcuts 之 render()
- 基于 Django1.10 文档的深入学习(12)—— django.shortcuts 之 redirect()
- 基于 Django1.10 文档的深入学习(13)—— django.core.urlresolvers 之 reverse()
- 基于 Django1.10 文档的深入学习(16)——Authentication backends 之 class ModelBackend
- 基于 Django1.10 文档的深入学习(22)——Sending email 之 send_mail()
- 基于 Django1.10 文档的深入学习(23)—— QuerySet API reference 之 icontains
- MySQL
- TensorFlow笔记之基础总结
- java中static修饰符
- LeetCode 143. Reorder List
- PS快捷键
- 基于 Django1.10 文档的深入学习(14)—— Request and response objects 之 HttpResponse objects
- springmvc(二)---controller中方法的三种返回值形式
- 分布式业务系统的一致性
- [BZOJ1177][Apio2009]Oil(暴力模拟)
- hibernate4 批量删除
- Adapter 适配器模式
- 电路原理实验八:受控源特性测试
- vue百度下拉框
- equals()