aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide/renderers.md
diff options
context:
space:
mode:
authorTom Christie2012-10-09 15:58:48 +0100
committerTom Christie2012-10-09 15:58:48 +0100
commit97a7f27c8219181e40dddcaf820545e08283de93 (patch)
treea81297e834cf40b216ed29a487d083b1a125ffb7 /docs/api-guide/renderers.md
parentce21fa1dc6dd8c941b71d9219360b3e9083051d4 (diff)
downloaddjango-rest-framework-97a7f27c8219181e40dddcaf820545e08283de93.tar.bz2
Rename HTMLTemplateRenderer -> HTMLRenderer, DocuemntingHTMLRenderer -> BrowseableAPIRenderer
Diffstat (limited to 'docs/api-guide/renderers.md')
-rw-r--r--docs/api-guide/renderers.md58
1 files changed, 43 insertions, 15 deletions
diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md
index 2b1f423d..5d93ea38 100644
--- a/docs/api-guide/renderers.md
+++ b/docs/api-guide/renderers.md
@@ -23,7 +23,7 @@ The default set of renderers may be set globally, using the `DEFAULT_RENDERERS`
REST_FRAMEWORK = {
'DEFAULT_RENDERERS': (
'rest_framework.renderers.YAMLRenderer',
- 'rest_framework.renderers.DocumentingHTMLRenderer',
+ 'rest_framework.renderers.BrowsableAPIRenderer',
)
}
@@ -88,20 +88,12 @@ If your API includes views that can serve both regular webpages and API response
**.format:** `'.xml'`
-## 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'`
-
-## HTMLTemplateRenderer
+## HTMLRenderer
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 HTMLRenderer 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):
@@ -109,18 +101,54 @@ The template name is determined by (in order of preference):
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.
+An example of a view that uses `HTMLRenderer`:
-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.
+ class UserInstance(generics.RetrieveUserAPIView):
+ """
+ A view that returns a templated HTML representations of a given user.
+ """
+ model = Users
+ renderer_classes = (HTMLRenderer,)
+
+ def get(self, request, \*args, **kwargs)
+ self.object = self.get_object()
+ return Response(self.object, template_name='user_detail.html')
+
+You can use `HTMLRenderer` 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 `HTMLRenderer` along with other renderer classes, you should consider listing `HTMLRenderer` 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`
**.format:** `'.html'`
+## BrowsableAPIRenderer
+
+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'`
+
## Custom renderers
To implement a custom renderer, you should override `BaseRenderer`, set the `.media_type` and `.format` properties, and implement the `.render(self, data, media_type)` method.
+For example:
+
+ from django.utils.encoding import smart_unicode
+ from rest_framework import renderers
+
+
+ class PlainText(renderers.BaseRenderer):
+ media_type = 'text/plain'
+ format = 'txt'
+
+ def render(self, data, media_type):
+ if isinstance(data, basestring):
+ return data
+ return smart_unicode(data)
+
---
# Advanced renderer usage
@@ -139,7 +167,7 @@ In some cases you might want your view to use different serialization styles dep
For example:
@api_view(('GET',))
- @renderer_classes((TemplateHTMLRenderer, JSONRenderer))
+ @renderer_classes((HTMLRenderer, JSONRenderer))
def list_users(request):
"""
A view that can return JSON or HTML representations
@@ -173,4 +201,4 @@ For good examples of custom media types, see GitHub's use of a custom [applicati
[HATEOAS]: http://timelessrepo.com/haters-gonna-hateoas
[quote]: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven
[application/vnd.github+json]: http://developer.github.com/v3/media/
-[application/vnd.collection+json]: http://www.amundsen.com/media-types/collection/ \ No newline at end of file
+[application/vnd.collection+json]: http://www.amundsen.com/media-types/collection/