django的HttpResponse对象

HttpResponse objects

class HttpResponse

相对于django自动创建的HttpRequest对象,HttpResponse对象需要去我们自己创建,每个view都也应该返回一个HttpResponse对象,
HttpResponse class在django.http模块中

用处

  1. 传递字符串

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

2.传递迭代器


属性

  1. HttpResponse.content:bytestring
  2. HttpResponse.charset:字符集
  3. HttpResponse.status_code:status_code
  4. HttpResponse.reason_phrase:
  5. HttpResponse.streaming:总是false
  6. HttpResponse.closed:True if the response has been closed.

方法

  1. HttpResponse.init(content=”, content_type=None, status=200, reason=None, charset=None):
    初始化HttpResponse

    • 参数content:应该是字符串迭代器,如果不是迭代器,将返回字符串,如果都不是的话,将会转成字符串
    • 参数content_type:可选的MIME type
    • 参数status:状态码
    • 参数reason:HTTP response phrase
    • 参数charset:编码字符集
  2. HttpResponse.setitem(header, value):
    设置header和value。字符串

  3. HttpResponse.delitem(header):
    删除指定header
  4. HttpResponse.getitem(header):
    获取指定header
  5. HttpResponse.setdefault(header, value):
    设置指定header,除非已经设置过
  6. HttpResponse.set_cookie(key, value=”, max_age=None, expires=None, path=’/’, domain=None, secure=None, httponly=False):
    设置cookie,参数类似于class http.cookies.Morsel
  7. HttpResponse.set_signed_cookie(key, value, salt=”, max_age=None, expires=None, path=’/’, domain=None, secure=None, httponly=True):类似set_cookie
  8. HttpResponse.delete_cookie(key, path=’/’, domain=None):
  9. HttpResponse.write(content):
  10. HttpResponse.flush():
  11. HttpResponse.tell():
  12. HttpResponse.getvalue():False,返回流对象
  13. HttpResponse.readable():False,返回流对象
  14. HttpResponse.seekable():False,返回流对象
  15. HttpResponse.writable():True,返回流对象
  16. HttpResponse.writelines(lines):字符串列表。,返回流对象

子类

django.http包含许多HttpResponse subclasses,用于处理不同类型的HTTP responses

  1. class HttpResponseRedirect:

    构造函数的第一个参数(重定向的路径)是必须的,可以是全路径,没有域名的绝对路径,或者相似路径,status code = 302.

    `class HttpResponseRedirect(HttpResponseRedirectBase):
        status_code = 302
    `
    
  2. class HttpResponsePermanentRedirect:
    永久重定向,status code = 301

  3. class HttpResponseNotModified:
    构造函数不包含任何参数,页面未发生任何变化,status code = 304

  4. class HttpResponseBadReques:status code = 400

  5. class HttpResponseNotFound:status code = 404
  6. class HttpResponseForbidden:status code = 403
  7. class HttpResponseNotAllowed:status code = 405
    构造函数参数1为:a list of permitted methods(e.g. [‘GET’, ‘POST’]).
  8. class HttpResponseGone:status code = 410
  9. class HttpResponseServerError:status code = 500

JsonResponse

class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None, **kwargs)
HttpResponse子类,用于创建json格式的response。继承不部分父类的方法,与父类有两点不同:

  • 默认的Content-Type header 设置成application/json.
  • 参数一data,要求是字典类实例,如果safe设置成False,data可以是任意JSON-serializable对象

StreamingHttpResponse

class StreamingHttpResponse
生成流数据,当生成Response需要花费太多时间或者大量内存时使用,比如生成大型csv文件
StreamingHttpResponse并不是HttpResponse的子类,


FileResponse

StreamingHttpResponse的子类,操作作二进制文件,

>>> from django.http import FileResponse
>>> response = FileResponse(open('myfile.png', 'rb'))

文件会自动关闭

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值