From 2575ea92aad3608142cfdd3ede5ee1b53e2064ba Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 5 Oct 2012 13:04:34 +0100 Subject: [PATCH] Docs for template responses --- docs/api-guide/renderers.md | 19 +++++++++- docs/api-guide/responses.md | 56 +++++++++++++++++++++++++--- rest_framework/response.py | 4 +- rest_framework/tests/htmlrenderer.py | 2 +- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index ef0ed46f1..d2d8985df 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -86,11 +86,28 @@ If your API includes views that can serve both regular webpages and API response ## DocumentingHTMLRenderer +Renders data into HTML for the browseable API. This renderer will determine which other renderer would have been given highest priority, and use that to display an API style response within the HTML page. + **.media_type:** `text/html` **.format:** `'.api'` -## TemplateHTMLRenderer +## HTMLTemplateRenderer + +Renders data to HTML, using Django's standard template rendering. +Unlike other renderers, the data passed to the `Response` does not need to be serialized. Also, unlike other renderers, you may want to include a `template_name` argument when creating the `Response`. + +The HTMLTemplateRenderer will create a `RequestContext`, using the `response.data` as the context dict, and determine a template name to use to render the context. + +The template name is determined by (in order of preference): + +1. An explicit `.template_name` attribute set on the response. +2. An explicit `.template_name` attribute set on this class. +3. The return result of calling `view.get_template_names()`. + +You can use `HTMLTemplateRenderer` either to return regular HTML pages using REST framework, or to return both HTML and API responses from a single endpoint. + +If you're building websites that use `HTMLTemplateRenderer` along with other renderer classes, you should consider listing `HTMLTemplateRenderer` as the first class in the `renderer_classes` list, so that it will be prioritised first even for browsers that send poorly formed ACCEPT headers. **.media_type:** `text/html` diff --git a/docs/api-guide/responses.md b/docs/api-guide/responses.md index e9ebcf819..d8f8e97c3 100644 --- a/docs/api-guide/responses.md +++ b/docs/api-guide/responses.md @@ -14,7 +14,11 @@ There's no requirement for you to use the `Response` class, you can also return 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. -## Response(data, status=None, headers=None) +--- + +# Methods + +## Response(data, status=None, template_name=None, headers=None) 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. @@ -22,16 +26,58 @@ The renderers used by the `Response` class cannot natively handle complex dataty You can use REST framework's `Serializer` classes to perform this data serialization, or use your own custom serialization. +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 `HTMLTemplateRenderer` is selected. +* `headers`: A dictionary of HTTP headers to use in the response. + +## .render() + +This methd 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)` method on the accepted renderer instance. + +You won't typically need to call `.render()` yourself, as it's handled by Django's standard response cycle. + +## Standard HTTPResponse methods + +The `Response` class extends `SimpleTemplateResponse`, and all the usual 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' + +--- + +# Attributes + ## .data The unrendered content of a `Request` object can be accessed using the `.data` attribute. +## .status_code + +The numeric status code of the HTTP response. + ## .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. +The rendered content of the response. `.render()` must have been called before `.content` can be accessed. -## .renderer +## .template_name -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. +The `template_name`, if supplied. Only required if `HTMLTemplateRenderer` or some other custom template renderer is the accepted renderer for the reponse. -[cite]: https://docs.djangoproject.com/en/dev/ref/template-response/ \ No newline at end of file +## .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. + + +[cite]: https://docs.djangoproject.com/en/dev/ref/template-response/ +[statuscodes]: status-codes.md \ No newline at end of file diff --git a/rest_framework/response.py b/rest_framework/response.py index 796750fc4..9a633a8a7 100644 --- a/rest_framework/response.py +++ b/rest_framework/response.py @@ -8,7 +8,7 @@ class Response(SimpleTemplateResponse): arbitrary media types. """ - def __init__(self, data=None, status=None, + def __init__(self, data=None, status=200, template_name=None, headers=None): """ Alters the init arguments slightly. @@ -30,7 +30,7 @@ class Response(SimpleTemplateResponse): assert renderer, "No accepted renderer set on Response" assert media_type, "No accepted media type set on Response" - self['content-type'] = media_type + self['Content-Type'] = media_type if self.data is None: return renderer.render() diff --git a/rest_framework/tests/htmlrenderer.py b/rest_framework/tests/htmlrenderer.py index 2c672dd08..7a7f27439 100644 --- a/rest_framework/tests/htmlrenderer.py +++ b/rest_framework/tests/htmlrenderer.py @@ -47,4 +47,4 @@ class HTMLRendererTests(TestCase): def test_simple_html_view(self): response = self.client.get('/') self.assertContains(response, "example: foobar") - self.assertEquals(response['content-type'], 'text/html') + self.assertEquals(response['Content-Type'], 'text/html')