django-rest-framework/docs/api-guide/responses.md

99 lines
4.6 KiB
Markdown
Raw Normal View History

2019-07-01 05:04:36 +03:00
---
source:
- response.py
---
2012-09-09 01:06:13 +04:00
2012-09-01 23:26:27 +04:00
# Responses
2013-05-28 19:13:12 +04:00
> Unlike basic HttpResponse objects, TemplateResponse objects retain 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.
2012-09-01 23:26:27 +04:00
>
> — [Django documentation][cite]
REST framework supports HTTP content negotiation by providing a `Response` class which allows you to return content that can be rendered into multiple content types, depending on the client request.
The `Response` class subclasses Django's `SimpleTemplateResponse`. `Response` objects are initialised with data, which should consist of native Python primitives. REST framework then uses standard HTTP content negotiation to determine how it should render the final response content.
2012-09-01 23:26:27 +04:00
There's no requirement for you to use the `Response` class, you can also return regular `HttpResponse` or `StreamingHttpResponse` objects from your views if required. Using the `Response` class simply provides a nicer interface for returning content-negotiated Web API responses, that can be rendered to multiple formats.
2012-09-01 23:26:27 +04:00
2012-09-30 18:55:24 +04:00
Unless you want to heavily customize REST framework for some reason, you should always use an `APIView` class or `@api_view` function for views that return `Response` objects. Doing so ensures that the view can perform content negotiation and select the appropriate renderer for the response, before it is returned from the view.
2012-09-01 23:26:27 +04:00
2012-10-05 16:04:34 +04:00
---
2012-10-14 23:46:38 +04:00
# Creating responses
2012-10-05 16:04:34 +04:00
2012-10-05 17:00:02 +04:00
## Response()
**Signature:** `Response(data, status=None, template_name=None, headers=None, content_type=None)`
2012-09-01 23:26:27 +04:00
Unlike regular `HttpResponse` objects, you do not instantiate `Response` objects with rendered content. Instead you pass in unrendered data, which may consist of any Python primitives.
2012-09-01 23:26:27 +04:00
2013-08-07 22:00:06 +04:00
The renderers used by the `Response` class cannot natively handle complex datatypes such as Django model instances, so you need to serialize the data into primitive datatypes before creating the `Response` object.
2012-09-01 23:26:27 +04:00
2012-09-30 18:55:24 +04:00
You can use REST framework's `Serializer` classes to perform this data serialization, or use your own custom serialization.
2012-09-01 23:26:27 +04:00
2012-10-05 16:04:34 +04:00
Arguments:
* `data`: The serialized data for the response.
* `status`: A status code for the response. Defaults to 200. See also [status codes][statuscodes].
* `template_name`: A template name to use if `HTMLRenderer` is selected.
2012-10-05 16:04:34 +04:00
* `headers`: A dictionary of HTTP headers to use in the response.
* `content_type`: The content type of the response. Typically, this will be set automatically by the renderer as determined by content negotiation, but there may be some cases where you need to specify the content type explicitly.
2012-10-05 16:04:34 +04:00
---
# Attributes
2012-09-30 18:55:24 +04:00
## .data
The unrendered, serialized data of the response.
2012-09-30 18:55:24 +04:00
2012-10-05 16:04:34 +04:00
## .status_code
The numeric status code of the HTTP response.
2012-09-30 18:55:24 +04:00
## .content
2012-10-05 18:22:30 +04:00
The rendered content of the response. The `.render()` method must have been called before `.content` can be accessed.
2012-10-05 16:04:34 +04:00
## .template_name
2013-08-07 22:00:06 +04:00
The `template_name`, if supplied. Only required if `HTMLRenderer` or some other custom template renderer is the accepted renderer for the response.
2012-10-05 16:04:34 +04:00
## .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.
2012-09-30 18:55:24 +04:00
2012-10-05 16:04:34 +04:00
Set automatically by the `APIView` or `@api_view` immediately before the response is returned from the view.
2012-09-30 18:55:24 +04:00
## .renderer_context
A dictionary of additional context information that will be passed to the renderer's `.render()` method.
Set automatically by the `APIView` or `@api_view` immediately before the response is returned from the view.
2012-09-01 23:26:27 +04:00
2012-10-14 23:46:38 +04:00
---
# Standard HttpResponse attributes
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()
**Signature:** `.render()`
2012-10-21 18:34:07 +04:00
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.
2012-10-14 23:46:38 +04:00
You won't typically need to call `.render()` yourself, as it's handled by Django's standard response cycle.
2016-11-30 15:58:34 +03:00
[cite]: https://docs.djangoproject.com/en/stable/stable/template-response/
[statuscodes]: status-codes.md