码迷,mamicode.com
首页 > Windows程序 > 详细

API Guide(二)之Responses

时间:2017-05-27 20:08:17      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:返回   amp   处理   needed   head   客户端   require   数字   prim   

Responses

与基本的HttpResponse对象不同,TemplateResponse对象保留 the details of the context that was provided by the view to compute the response。The final output of the response is not computed until it is needed, later in the response process.

Django文档

REST框架通过提供一个Response类来支持HTTP content negotiation,该类允许您根据客户端请求返回可以被渲染成多种内容类型的内容。

The Response class subclasses Django‘s SimpleTemplateResponseResponse objects are initialised with data, which should consist of native Python primitives.然后,REST框架使用标准HTTP内容协商来确定如何渲染最终响应内容。

您不需要使用Response该类,如果需要,还可以从视图中返回常规的HttpResponseStreamingHttpResponse对象。使用Response该类只需提供一个更好的界面来返回内容协商的Web API响应,这个界面可以渲染为多种格式。

除非您想要大量自定义REST框架,否则您应该始终为返回Response 对象的视图使用APIView类或@api_view函数。这样做可以确保在从视图返回之前可以执行 content negotiation并为响应选择适当的渲染器。


Creating responses

Response()

签名: Response(data, status=None, template_name=None, headers=None, content_type=None)

与常规HttpResponse对象不同,您不会用 rendered content 实例化 Response对象。而是传递 unrendered data,这些数据可能由任何Python primitives 组成。

Response类使用的renderers 不能本地处理诸如Django模型实例等复杂的数据类型,因此您需要在创建Response对象之前将数据序列化为原始数据类型。

您可以使用REST框架的Serializer类来执行此数据序列化,或使用您自定义的serialization。

参数:

  • data:响应的序列化数据。
  • status:响应的状态码。默认为200.另请参见状态码
  • template_nameHTMLRenderer选择使用的模板名称。
  • headers:用于响应的HTTP标头字典。
  • content_type:响应的内容类型。通常,这将由content negotiation确定由renderer 自动设置,但在某些情况下,需要你明确指定内容类型。

Attributes

.data

Request对象的未渲染内容。

.status_code

HTTP响应的数字形式的状态码。

.content

The rendered content of the response。.render()方法必须在.content被访问之前被调用。

.template_name

The template_name, if supplied. Only required if HTMLRenderer or some other custom template renderer is the accepted renderer for the response.

.accepted_renderer

将用于渲染响应的渲染器实例。

从视图返回响应之前,由APIView@api_view自动设置。

.accepted_media_type

由the content negotiation stage 选择的媒体类型。

从视图返回响应之前,由APIView@api_view自动设置。

.renderer_context

将传递给渲染器.render()方法的附加上下文信息的字典。

从视图返回响应之前,由APIView@api_view自动设置。


标准的HttpResponse属性

The Response class extends SimpleTemplateResponse, and all the usual attributes and methods are also available on the response. For example you can set headers on the response in the standard way:

response = Response()
response[‘Cache-Control‘] = ‘no-cache‘

.render()

签名: .render()

As with any other TemplateResponse, this method is called to render the serialized data of the response into the final response content. When .render() is called, the response content will be set to the result of calling the .render(data, accepted_media_type, renderer_context) method on the accepted_renderer instance.

通常您不需要调用.render()自己,因为它是由Django的标准响应周期处理的。

API Guide(二)之Responses

标签:返回   amp   处理   needed   head   客户端   require   数字   prim   

原文地址:http://www.cnblogs.com/yangxiaoling/p/6914095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!