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

37 lines
2.6 KiB
Markdown
Raw Normal View History

2012-09-09 01:06:13 +04:00
<a class="github" href="response.py"></a>
2012-09-01 23:26:27 +04:00
# Responses
> 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.
>
> &mdash; [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.
2012-09-30 18:55:24 +04:00
The `Response` class subclasses Django's `SimpleTemplateResponse`. `Response` objects are initialised with data, which should consist of native python primatives. 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
2012-09-30 18:55:24 +04:00
There's no requirement for you to use the `Response` class, you can also return regular `HttpResponse` objects from your views if you want, but it provides a nicer interface for returning Web API responses.
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-09-30 18:55:24 +04:00
## Response(data, status=None, headers=None)
2012-09-01 23:26:27 +04:00
2012-09-30 18:55:24 +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 primatives.
2012-09-01 23:26:27 +04:00
2012-09-30 18:55:24 +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 primative 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-09-30 18:55:24 +04:00
## .data
The unrendered content of a `Request` object can be accessed using the `.data` attribute.
## .content
To access the rendered content of a `Response` object, you must first call `.render()`. You'll typically only need to do this in cases such as unit testing responses - when you return a `Response` from a view Django's response cycle will handle calling `.render()` for you.
## .renderer
When you return a `Response` instance, the `APIView` class or `@api_view` decorator will select the appropriate renderer, and set the `.renderer` attribute on the `Response`, before returning it from the view.
2012-09-01 23:26:27 +04:00
[cite]: https://docs.djangoproject.com/en/dev/ref/template-response/