diff options
| author | Tom Christie | 2012-10-15 13:27:50 +0100 |
|---|---|---|
| committer | Tom Christie | 2012-10-15 13:27:50 +0100 |
| commit | 9c1fba3483b7e81da0744464dcf23a5f12711de2 (patch) | |
| tree | d9370dc9fb9d2fea65192bf5ce4d7fb594d3ad0c /docs/api-guide/renderers.md | |
| parent | e88ca9637bd4f49659dd80ca7afd0f38adf07746 (diff) | |
| download | django-rest-framework-9c1fba3483b7e81da0744464dcf23a5f12711de2.tar.bz2 | |
Tweak parsers to take parser_context
Diffstat (limited to 'docs/api-guide/renderers.md')
| -rw-r--r-- | docs/api-guide/renderers.md | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index 024a4ee2..c8addb32 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -98,7 +98,7 @@ Renders the request data into `YAML`. Renders REST framework's default style of `XML` response content. -Note that the `XML` markup language is used typically used as the base language for more strictly defined domain-specific languages, such as `RSS`, `Atom`, `SOAP`, and `XHTML`. +Note that the `XML` markup language is used typically used as the base language for more strictly defined domain-specific languages, such as `RSS`, `Atom`, and `XHTML`. If you are considering using `XML` for your API, you may want to consider implementing a custom renderer and parser for your specific requirements, and using an existing domain-specific media-type, or creating your own custom XML-based media-type. @@ -154,21 +154,6 @@ Renders data into HTML for the Browseable API. This renderer will determine whi To implement a custom renderer, you should override `BaseRenderer`, set the `.media_type` and `.format` properties, and implement the `.render(self, data, media_type=None, renderer_context=None)` 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=None, renderer_context=None): - if isinstance(data, basestring): - return data - return smart_unicode(data) - The arguments passed to the `.render()` method are: ### `data` @@ -184,6 +169,23 @@ Optional. If provided, this is the accepted media type, as determined by the con Optional. If provided, this is a dictionary of contextual information provided by the view. By default this will include the following keys: `view`, `request`, `response`, `args`, `kwargs`. +## Example + +The following is an example plaintext renderer that will return a response with the `data` parameter as the content of the response. + + 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=None, renderer_context=None): + if isinstance(data, basestring): + return data + return smart_unicode(data) + --- # Advanced renderer usage |
