diff options
| author | Tom Christie | 2012-10-28 12:42:10 -0700 |
|---|---|---|
| committer | Tom Christie | 2012-10-28 12:42:10 -0700 |
| commit | 3d3df22d822774f7f73291f5071bdfa7ce552748 (patch) | |
| tree | 7f60a61ba6fecb948f66ffe2c8239084a02d43a5 /rest_framework/renderers.py | |
| parent | 1b2c2358476fd7948f87c5c161344a44de28275f (diff) | |
| parent | db635fa6327d8d3ac3b06886c5f459b5c5a5cd04 (diff) | |
| download | django-rest-framework-3d3df22d822774f7f73291f5071bdfa7ce552748.tar.bz2 | |
Merge pull request #330 from tomchristie/tutorial-refactor
Tutorial refactor
Diffstat (limited to 'rest_framework/renderers.py')
| -rw-r--r-- | rest_framework/renderers.py | 41 |
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. |
