response.py

Responses

与基本的HttpResponse对象不同,TemplateResponse对象保留视图提供的上下文的详细信息以计算响应。 在响应过程中稍后需要时,不会计算响应的最终输出。

Django documentation

REST框架通过提供Response类来支持HTTP内容协商,该类允许您返回可以呈现为多种内容类型的内容,具体取决于客户端请求。

Response类是Django的SimpleTemplateResponse的子类。 Response对象使用数据初始化,数据应包含本机Python基元。 然后,REST框架使用标准HTTP内容协商来确定它应如何呈现最终响应内容。

您无需使用Response类,如果需要,还可以从视图中返回常规HttpResponseStreamingHttpResponse对象。 使用Response类只是为返回内容协商的Web API响应提供了一个更好的界面,可以将其呈现为多种格式。

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


Creating responses

Response()

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

与常规HttpResponse对象不同,您不会使用呈现的内容实例化Response对象。 而是传入未呈现的数据,这些数据可能包含任何Python原语。

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

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

Arguments:

  • data:响应的序列化数据。
  • status:响应的状态代码。 Defaults to 200. See also status codes.
  • template_name:如果选择了HTMLRenderer,则使用的模板名称。
  • headers:要在响应中使用的HTTP标头的字典。
  • content_type:响应的内容类型。 通常,这将由内容协商确定的渲染器自动设置,但在某些情况下您可能需要明确指定内容类型。

Attributes

.data

响应的未呈现的序列化数据。

.status_code

HTTP响应的数字状态代码。

.content

呈现的响应内容。 必须在.content访问之前调用.render()方法。

.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

The renderer instance that will be used to render the response.

Set automatically by the APIView or @api_view immediately before the response is returned from the view.

.accepted_media_type

The media type that was selected by the content negotiation stage.

Set automatically by the APIView or @api_view immediately before the response is returned from the view.

.renderer_context

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

Set automatically by the APIView or @api_view immediately before the response is returned from the view.


Standard HttpResponse attributes

Response类扩展了SimpleTemplateResponse,并且响应中也提供了所有常用的属性和方法。 例如,您可以按标准方式在响应上设置标题:

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

.render()

Signature: .render()

与任何其他TemplateResponse一样,调用此方法可将响应的序列化数据呈现到最终响应内容中。 当调用.render()时,响应内容将被设置为在accepted_renderer上调用.render(data,accepted_media_type,renderer_context)方法的结果实例。

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