diff options
| author | Tom Christie | 2012-10-14 22:43:07 +0100 | 
|---|---|---|
| committer | Tom Christie | 2012-10-14 22:43:07 +0100 | 
| commit | 551c86c43a71f7dee5cce68c5142714301f6196f (patch) | |
| tree | b9303172e27ea71b90dadf4c3a0dc303c1c0972e /docs/api-guide/renderers.md | |
| parent | 7a89d7a770d310f8b72178c561fcd04e6c15e5c4 (diff) | |
| download | django-rest-framework-551c86c43a71f7dee5cce68c5142714301f6196f.tar.bz2 | |
Documentation for parsers
Diffstat (limited to 'docs/api-guide/renderers.md')
| -rw-r--r-- | docs/api-guide/renderers.md | 57 | 
1 files changed, 39 insertions, 18 deletions
| diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index b2ebd0c7..024a4ee2 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -42,8 +42,8 @@ You can also set the renderers used for an individual view, using the `APIView`  Or, if you're using the `@api_view` decorator with function based views. -    @api_view('GET'), -    @renderer_classes(JSONRenderer, JSONPRenderer) +    @api_view(('GET',)), +    @renderer_classes((JSONRenderer, JSONPRenderer))      def user_count_view(request, format=None):          """          A view that returns the count of active users, in JSON or JSONp. @@ -66,27 +66,45 @@ If your API includes views that can serve both regular webpages and API response  ## JSONRenderer -**.media_type:** `application/json` +Renders the request data into `JSON`. -**.format:** `'.json'` +The client may additionally include an `'indent'` media type parameter, in which case the returned `JSON` will be indented.  For example `Accept: application/json; indent=4`. + +**.media_type**: `application/json` + +**.format**: `'.json'`  ## JSONPRenderer -**.media_type:** `application/javascript` +Renders the request data into `JSONP`.  The `JSONP` media type provides a mechanism of allowing cross-domain AJAX requests, by wrapping a `JSON` response in a javascript callback. + +The javascript callback function must be set by the client including a `callback` URL query parameter.  For example `http://example.com/api/users?callback=jsonpCallback`.  If the callback function is not explicitly set by the client it will default to `'callback'`. -**.format:** `'.jsonp'` +**Note**: If you require cross-domain AJAX requests, you may also want to consider using [CORS] as an alternative to `JSONP`. + +**.media_type**: `application/javascript` + +**.format**: `'.jsonp'`  ## YAMLRenderer -**.media_type:** `application/yaml` +Renders the request data into `YAML`.  + +**.media_type**: `application/yaml` -**.format:** `'.yaml'` +**.format**: `'.yaml'`  ## XMLRenderer -**.media_type:** `application/xml` +Renders REST framework's default style of `XML` response content. -**.format:** `'.xml'` +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`. + +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. + +**.media_type**: `application/xml` + +**.format**: `'.xml'`  ## HTMLRenderer @@ -118,19 +136,21 @@ You can use `HTMLRenderer` either to return regular HTML pages using REST framew  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` +**.media_type**: `text/html` -**.format:** `'.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` +**.media_type**: `text/html` -**.format:** `'.api'` +**.format**: `'.api'` + +--- -## Custom renderers +# 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=None, renderer_context=None)` method. @@ -151,15 +171,15 @@ For example:  The arguments passed to the `.render()` method are: -#### `data` +### `data`  The request data, as set by the `Response()` instantiation. -#### `media_type=None` +### `media_type=None`  Optional. If provided, this is the accepted media type, as determined by the content negotiation stage.  Depending on the client's `Accept:` header, this may be more specific than the renderer's `media_type` attribute, and may include media type parameters.  For example `"application/json; nested=true"`. -#### `renderer_context=None` +### `renderer_context=None`  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`. @@ -213,6 +233,7 @@ For good examples of custom media types, see GitHub's use of a custom [applicati  [cite]: https://docs.djangoproject.com/en/dev/ref/template-response/#the-rendering-process  [conneg]: content-negotiation.md  [browser-accept-headers]: http://www.gethifi.com/blog/browser-rest-http-accept-headers +[CORS]: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing  [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/ | 
