aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/renderers.py
diff options
context:
space:
mode:
authorTom Christie2012-10-28 18:12:56 +0000
committerTom Christie2012-10-28 18:12:56 +0000
commit12c363c1fe237d0357e6020b44890926856b9191 (patch)
tree54e66f607c2df1d096b61621b6d0dd1e2f360d68 /rest_framework/renderers.py
parent44207a347ac765d900f15b65bdd24dbf8eb9ead2 (diff)
downloaddjango-rest-framework-12c363c1fe237d0357e6020b44890926856b9191.tar.bz2
TemplateHTMLRenderer, StaticHTMLRenderer
Diffstat (limited to 'rest_framework/renderers.py')
-rw-r--r--rest_framework/renderers.py41
1 files changed, 36 insertions, 5 deletions
diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py
index 1a8b1d97..cfe4df6d 100644
--- a/rest_framework/renderers.py
+++ b/rest_framework/renderers.py
@@ -139,13 +139,24 @@ class YAMLRenderer(BaseRenderer):
return yaml.dump(data, stream=None, Dumper=self.encoder)
-class HTMLRenderer(BaseRenderer):
+class TemplateHTMLRenderer(BaseRenderer):
"""
- A Base class provided for convenience.
+ An HTML renderer for use with templates.
- Render the object simply by using the given template.
- To create a template renderer, subclass this class, and set
- the :attr:`media_type` and :attr:`template` attributes.
+ The data supplied to the Response object should be a dictionary that will
+ be used as context for the template.
+
+ 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()`.
+
+ For example:
+ data = {'users': User.objects.all()}
+ return Response(data, template_name='users.html')
+
+ For pre-rendered HTML, see StaticHTMLRenderer.
"""
media_type = 'text/html'
@@ -188,6 +199,26 @@ class HTMLRenderer(BaseRenderer):
raise ConfigurationError('Returned a template response with no template_name')
+class StaticHTMLRenderer(BaseRenderer):
+ """
+ An HTML renderer class that simply returns pre-rendered HTML.
+
+ The data supplied to the Response object should be a string representing
+ the pre-rendered HTML content.
+
+ For example:
+ data = '<html><body>example</body></html>'
+ return Response(data)
+
+ For template rendered HTML, see TemplateHTMLRenderer.
+ """
+ media_type = 'text/html'
+ format = 'html'
+
+ def render(self, data, accepted_media_type=None, renderer_context=None):
+ return data
+
+
class BrowsableAPIRenderer(BaseRenderer):
"""
HTML renderer used to self-document the API.