rest_framework.renderers

Renderers are used to serialize a response into specific media types.

They give us a generic way of being able to handle various media types on the response, such as JSON encoded data or HTML output.

REST framework also provides an HTML renderer that renders the browsable API.

Module Contents

zero_as_none(value)
class BaseRenderer

Bases:object

All renderers should extend this class, setting the media_type and format attributes, and override the .render() method.

media_type
format
charset = utf-8
render_style = text
render(self, data, accepted_media_type=None, renderer_context=None)
class JSONRenderer

Bases:rest_framework.renderers.BaseRenderer

Renderer which serializes to JSON.

media_type = application/json
format = json
encoder_class
ensure_ascii
compact
strict
charset
get_indent(self, accepted_media_type, renderer_context)
render(self, data, accepted_media_type=None, renderer_context=None)

Render data into JSON, returning a bytestring.

class TemplateHTMLRenderer

Bases:rest_framework.renderers.BaseRenderer

An HTML renderer for use with templates.

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
format = html
template_name
exception_template_names = ['%(status_code)s.html', 'api_exception.html']
charset = utf-8
render(self, data, accepted_media_type=None, renderer_context=None)

Renders data to HTML, using Django’s standard template rendering.

The template name is determined by (in order of preference):

  1. An explicit .template_name set on the response.
  2. An explicit .template_name set on this class.
  3. The return result of calling view.get_template_names().
resolve_template(self, template_names)
get_template_context(self, data, renderer_context)
get_template_names(self, response, view)
get_exception_template(self, response)
class StaticHTMLRenderer

Bases:rest_framework.renderers.TemplateHTMLRenderer

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
charset = utf-8
render(self, data, accepted_media_type=None, renderer_context=None)
class HTMLFormRenderer

Bases:rest_framework.renderers.BaseRenderer

Renderers serializer data into an HTML form.

If the serializer was instantiated without an object then this will return an HTML form not bound to any object, otherwise it will return an HTML form with the appropriate initial data populated from the object.

Note that rendering of field and form errors is not currently supported.

media_type = text/html
format = form
charset = utf-8
template_pack = rest_framework/vertical/
base_template = form.html
default_style
render_field(self, field, parent_style)
render(self, data, accepted_media_type=None, renderer_context=None)

Render serializer data and return an HTML form, as a string.

class BrowsableAPIRenderer

Bases:rest_framework.renderers.BaseRenderer

HTML renderer used to self-document the API.

media_type = text/html
format = api
template = rest_framework/api.html
filter_template = rest_framework/filters/base.html
code_style = emacs
charset = utf-8
form_renderer_class
get_default_renderer(self, view)

Return an instance of the first valid renderer. (Don’t use another documenting renderer.)

get_content(self, renderer, data, accepted_media_type, renderer_context)

Get the content as if it had been rendered by the default non-documenting renderer.

show_form_for_method(self, view, method, request, obj)

Returns True if a form should be shown for this method.

_get_serializer(self, serializer_class, view_instance, request, *args, **kwargs)
get_rendered_html_form(self, data, view, method, request)

Return a string representing a rendered HTML form, possibly bound to either the input or output data.

In the absence of the View having an associated form then return None.

render_form_for_serializer(self, serializer)
get_raw_data_form(self, data, view, method, request)

Returns a form that allows for arbitrary content types to be tunneled via standard HTML forms. (Which are typically application/x-www-form-urlencoded)

get_name(self, view)
get_description(self, view, status_code)
get_breadcrumbs(self, request)
get_extra_actions(self, view)
get_filter_form(self, data, view, request)
get_context(self, data, accepted_media_type, renderer_context)

Returns the context used to render.

render(self, data, accepted_media_type=None, renderer_context=None)

Render the HTML for the browsable API representation.

class AdminRenderer

Bases:rest_framework.renderers.BrowsableAPIRenderer

template = rest_framework/admin.html
format = admin
render(self, data, accepted_media_type=None, renderer_context=None)
get_context(self, data, accepted_media_type, renderer_context)

Render the HTML for the browsable API representation.

get_result_url(self, result, view)

Attempt to reverse the result’s detail view URL.

This only works with views that are generic-like (has .lookup_field) and viewset-like (has .basename / .reverse_action()).

class DocumentationRenderer

Bases:rest_framework.renderers.BaseRenderer

media_type = text/html
format = html
charset = utf-8
template = rest_framework/docs/index.html
error_template = rest_framework/docs/error.html
code_style = emacs
languages = ['shell', 'javascript', 'python']
get_context(self, data, request)
render(self, data, accepted_media_type=None, renderer_context=None)
class SchemaJSRenderer

Bases:rest_framework.renderers.BaseRenderer

media_type = application/javascript
format = javascript
charset = utf-8
template = rest_framework/schema.js
render(self, data, accepted_media_type=None, renderer_context=None)
class MultiPartRenderer

Bases:rest_framework.renderers.BaseRenderer

media_type = multipart/form-data; boundary=BoUnDaRyStRiNg
format = multipart
charset = utf-8
BOUNDARY = BoUnDaRyStRiNg
render(self, data, accepted_media_type=None, renderer_context=None)
class CoreJSONRenderer

Bases:rest_framework.renderers.BaseRenderer

media_type = application/coreapi+json
charset
format = corejson
render(self, data, media_type=None, renderer_context=None)
class _BaseOpenAPIRenderer
get_schema(self, instance)
get_parameters(self, link)
get_operation(self, link, name, tag)
get_paths(self, document)
get_structure(self, data)
class OpenAPIRenderer

Bases:rest_framework.renderers._BaseOpenAPIRenderer

media_type = application/vnd.oai.openapi
charset
format = openapi
render(self, data, media_type=None, renderer_context=None)
class JSONOpenAPIRenderer

Bases:rest_framework.renderers._BaseOpenAPIRenderer

media_type = application/vnd.oai.openapi+json
charset
format = openapi-json
render(self, data, media_type=None, renderer_context=None)