mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Docs for template responses
This commit is contained in:
parent
26c7d6df6c
commit
2575ea92aa
|
@ -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`
|
||||
|
||||
|
|
|
@ -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/
|
||||
## .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
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user