Previous topic

permissions

Next topic

resource

This Page

renderers

The renderers module provides a set of renderers that can be plugged in to a Resource. A renderer is responsible for taking the output of a View and serializing it to a given media type. A Resource can have a number of renderers, allow the same content to be serialized in a number of different formats depending on the requesting client’s preferences, as specified in the HTTP Request’s Accept header.

Renderers are used to serialize a View’s output into specific media types.

Django REST framework also provides HTML and PlainText renderers that help self-document the API, by serializing the output along with documentation regarding the View, output status and headers, and providing forms and links depending on the allowed methods, renderers and parsers on the View.

class renderers.BaseRenderer(view)[source]

All renderers must extend this class, set the media_type attribute, and override the render() method.

can_handle_response(accept)[source]

Returns True if this renderer is able to deal with the given accept media type.

The default implementation for this function is to check the accept argument against the media_type attribute set on the class to see if they match.

This may be overridden to provide for other behavior, but typically you’ll instead want to just set the media_type attribute on the class.

render(obj=None, media_type=None)[source]

Given an object render it into a string.

The requested media type is also passed to this method, as it may contain parameters relevant to how the parser should render the output. EG: application/json; indent=4

By default render simply returns the output as-is. Override this method to provide for other behavior.

class renderers.JSONRenderer(view)[source]

Renderer which serializes to JSON

render(obj=None, media_type=None)[source]

Renders obj into serialized JSON.

class renderers.JSONPRenderer(view)[source]

Renderer which serializes to JSONP

renderer_class

alias of JSONRenderer

class renderers.XMLRenderer(view)[source]

Renderer which serializes to XML.

render(obj=None, media_type=None)[source]

Renders obj into serialized XML.

class renderers.TemplateRenderer(view)[source]

A Base class provided for convenience.

Render the object simply by using the given template. To create a template renderer, subclass this class, and set the media_type and template attributes.

render(obj=None, media_type=None)[source]

Renders obj using the template specified on the class.

class renderers.DocumentingHTMLRenderer(view)[source]

Renderer which provides a browsable HTML interface for an API. See the examples at http://api.django-rest-framework.org to see this in action.

class renderers.DocumentingXHTMLRenderer(view)[source]

Identical to DocumentingHTMLRenderer, except with an xhtml media type. We need this to be listed in preference to xml in order to return HTML to WebKit based browsers, given their Accept headers.

class renderers.DocumentingPlainTextRenderer(view)[source]

Renderer that serializes the object with the default renderer, but also provides plain-text documentation of the returned status and headers, and of the resource’s name and description. Useful for browsing an API with command line tools.