How the renderer is determined
The set of valid renderers for a view is always defined as a list of classes. When a view is entered, REST framework will perform content negotiation on the incoming request, and determine the most appropriate renderer to satisfy the request. The basic process of content negotiation involves examining the request’sAccept header, to determine which media types it expects in the response. Optionally, format suffixes on the URL may be used to explicitly request a particular representation (e.g., http://example.com/api/users.json).
For more information see the content negotiation documentation.
Setting the renderers
The default set of renderers may be set globally, using theDEFAULT_RENDERER_CLASSES setting:
APIView class-based views:
@api_view decorator:
Ordering of renderer classes
It’s important when specifying the renderer classes for your API to think about what priority you want to assign to each media type. If a client underspecifies the representations it can accept (e.g.,Accept: */*), then REST framework will select the first renderer in the list to use for the response.
Built-in Renderers
BaseRenderer
rest_framework.renderers.BaseRenderer
All renderers should extend this class, setting the media_type and format attributes, and override the .render() method.
Attributes
The media type that this renderer produces (e.g.,
'application/json')The format string used for format suffixes (e.g.,
'json')The character encoding to use. Set to
None for binary renderersEither
'text' or 'binary'. Used to determine how the browsable API displays the contentMethods
Render the response data and return a bytestring.Arguments:
data- The request data, as set by theResponse()instantiationaccepted_media_type- Optional. The accepted media type, as determined by content negotiationrenderer_context- Optional. A dictionary of contextual information (includesview,request,response,args,kwargsby default)
JSONRenderer
rest_framework.renderers.JSONRenderer
Renders the request data into JSON, using UTF-8 encoding.
Attributes
The media type produced by this renderer
The format string for format suffixes
None because JSON is a binary encoding (utf-8, utf-16, or utf-32)The JSON encoder class to use
Whether to escape non-ASCII characters (controlled by
UNICODE_JSON setting)Whether to use compact output (controlled by
COMPACT_JSON setting)Whether to use strict JSON encoding (controlled by
STRICT_JSON setting)Default style
Note that the default style includes unicode characters with no unnecessary whitespace:Indented output
The client may include an'indent' media type parameter, in which case the returned JSON will be indented:
Example
TemplateHTMLRenderer
rest_framework.renderers.TemplateHTMLRenderer
Renders data to HTML, using Django’s standard template rendering. Unlike other renderers, the data passed to the Response does not need to be serialized. Also, you may want to include a template_name argument when creating the Response.
Attributes
The media type produced by this renderer
The format string for format suffixes
The character encoding to use
The default template name to use
Template names to try when rendering exceptions:
['%(status_code)s.html', 'api_exception.html']Template name determination
The template name is determined by (in order of preference):- An explicit
template_nameargument passed to the response - An explicit
.template_nameattribute set on this class - The return result of calling
view.get_template_names()
When used with a view that makes use of a serializer, the
Response data may not be a dictionary and will need to be wrapped before returning:Example
StaticHTMLRenderer
rest_framework.renderers.StaticHTMLRenderer
A simple renderer that returns pre-rendered HTML. Unlike other renderers, the data passed to the response object should be a string representing the content to be returned.
Attributes
The media type produced by this renderer
The format string for format suffixes
The character encoding to use
Example
BrowsableAPIRenderer
rest_framework.renderers.BrowsableAPIRenderer
Renders data into HTML for the Browsable 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.
Attributes
The media type produced by this renderer
The format string for format suffixes
The character encoding to use
The template used to render the browsable API
The template used for filter forms
The Pygments code highlighting style
The renderer class to use for rendering forms
Customizing BrowsableAPIRenderer
You can customize the behavior by overriding theget_default_renderer() method:
AdminRenderer
rest_framework.renderers.AdminRenderer
Renders data into HTML for an admin-like display. This renderer is suitable for CRUD-style web APIs that should also present a user-friendly interface for managing the data.
Attributes
The media type produced by this renderer
The format string for format suffixes
The character encoding to use
The template used to render the admin interface
Example
HTMLFormRenderer
rest_framework.renderers.HTMLFormRenderer
Renders data returned by a serializer into an HTML form. The output does not include the enclosing <form> tags, hidden CSRF input, or submit buttons.
Attributes
The media type produced by this renderer
The format string for format suffixes
The character encoding to use
The template pack to use for rendering form fields
The base template for the form
This renderer is not intended to be used directly, but can be used in templates by passing a serializer instance to the
render_form template tag.Example
MultiPartRenderer
rest_framework.renderers.MultiPartRenderer
This renderer is used for rendering HTML multipart form data. It is not suitable as a response renderer, but is instead used for creating test requests.
Attributes
The media type produced by this renderer
The format string for format suffixes
The character encoding to use
OpenAPIRenderer
rest_framework.renderers.OpenAPIRenderer
Renders OpenAPI schema to YAML format.
Attributes
The media type produced by this renderer
The format string for format suffixes
None for binary outputJSONOpenAPIRenderer
rest_framework.renderers.JSONOpenAPIRenderer
Renders OpenAPI schema to JSON format.
Attributes
The media type produced by this renderer
The format string for format suffixes
None for binary outputThe JSON encoder class to use
Whether to escape non-ASCII characters
Custom Renderers
To implement a custom renderer, you should overrideBaseRenderer, set the .media_type and .format properties, and implement the .render(self, data, accepted_media_type=None, renderer_context=None) method.
The method should return a bytestring, which will be used as the body of the HTTP response.
Arguments
The request data, as set by the
Response() instantiationOptional. If provided, this is the accepted media type, as determined by the content negotiation stage. May include media type parameters (e.g.,
"application/json; nested=true").Optional. A dictionary of contextual information provided by the view. By default includes:
view, request, response, args, kwargs.Example
The following is an example plaintext renderer:Setting the character set
By default renderer classes use UTF-8 encoding. To use a different encoding, set thecharset attribute:
Binary renderers
If the renderer returns raw binary content, setcharset to None and optionally set render_style to 'binary':
Advanced Renderer Usage
Varying behavior by media type
You can accessrequest.accepted_renderer to determine the negotiated renderer:
Underspecifying the media type
You can use wildcard media types likeimage/* or */*. Make sure to specify the media type explicitly when returning the response:
