Django-- HttpResponse

       django的视图函数都要返回一个httpresponse对象,httpresponse存在于django.http模块中

1.将页面的内容当做一个string,传到httpresponse

>>>from django.http import HttpResponse
>>>response = HttpResponse('this is a test')
>>>response = HttpResponse('this is a file', content_type='application/text')

2.把httpresponse当做file-like object 使用

>>>response = HttpResponse()
>>>response.write("<p>Here's the text of the Web page.</p>")
>>>response.write("<p>Here's another paragraph.</p>")

3.你也可以给httpresponse传一个iterator. 当你给httpresponse传递一个iterator时, 它会立即使用这个iterator, 把它存储为一个string, 然后丢弃这个iterator.  如果你想把iterator中的内容作为流,传入到客户端中,你需要使用StreamingHttpResponse

4.设置response的header字段

你可以把response当做一个字典来删除,添加header字段

>>>response = HttpResponse()
>>>response['Age'] = 20
>>>del response['Age']
5.告诉浏览器把response当做一个附件

>>>response = HttpResponse(my_data, content_type='application/vnd.ms-excel')
>>>response['Content-Disposition'] = 'attachment; filename="foo.xls" '

6.属性

HttpResponse.content

一个二进制字符串

HttpResponse.charset

字符集编码格式

HttpResponse.status_code

response的状态码

HttpResponse.closed

如果response结束了,返回True






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值