HttpResponse objects
class HttpResponse
相对于django自动创建的HttpRequest对象,HttpResponse对象需要去我们自己创建,每个view都也应该返回一个HttpResponse对象,
HttpResponse class在django.http模块中
用处
传递字符串
>>> 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.传递迭代器
属性
- HttpResponse.content:bytestring
- HttpResponse.charset:字符集
- HttpResponse.status_code:status_code
- HttpResponse.reason_phrase:
- HttpResponse.streaming:总是false
- HttpResponse.closed:True if the response has been closed.
方法
HttpResponse.init(content=”, content_type=None, status=200, reason=None, charset=None):
初始化HttpResponse- 参数content:应该是字符串迭代器,如果不是迭代器,将返回字符串,如果都不是的话,将会转成字符串
- 参数content_type:可选的MIME type
- 参数status:状态码
- 参数reason:HTTP response phrase
- 参数charset:编码字符集
HttpResponse.setitem(header, value):
设置header和value。字符串- HttpResponse.delitem(header):
删除指定header - HttpResponse.getitem(header):
获取指定header - HttpResponse.setdefault(header, value):
设置指定header,除非已经设置过 - HttpResponse.set_cookie(key, value=”, max_age=None, expires=None, path=’/’, domain=None, secure=None, httponly=False):
设置cookie,参数类似于class http.cookies.Morsel - HttpResponse.set_signed_cookie(key, value, salt=”, max_age=None, expires=None, path=’/’, domain=None, secure=None, httponly=True):类似set_cookie
- HttpResponse.delete_cookie(key, path=’/’, domain=None):
- HttpResponse.write(content):
- HttpResponse.flush():
- HttpResponse.tell():
- HttpResponse.getvalue():False,返回流对象
- HttpResponse.readable():False,返回流对象
- HttpResponse.seekable():False,返回流对象
- HttpResponse.writable():True,返回流对象
- HttpResponse.writelines(lines):字符串列表。,返回流对象
子类
django.http包含许多HttpResponse subclasses,用于处理不同类型的HTTP responses
class HttpResponseRedirect:
构造函数的第一个参数(重定向的路径)是必须的,可以是全路径,没有域名的绝对路径,或者相似路径,status code = 302.
`class HttpResponseRedirect(HttpResponseRedirectBase): status_code = 302 `
class HttpResponsePermanentRedirect:
永久重定向,status code = 301class HttpResponseNotModified:
构造函数不包含任何参数,页面未发生任何变化,status code = 304class HttpResponseBadReques:status code = 400
- class HttpResponseNotFound:status code = 404
- class HttpResponseForbidden:status code = 403
- class HttpResponseNotAllowed:status code = 405
构造函数参数1为:a list of permitted methods(e.g. [‘GET’, ‘POST’]). - class HttpResponseGone:status code = 410
- 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'))
文件会自动关闭