Skip to content

Commit f96e2b7

Browse files
committed
Fix hierarchy of the renderers page
1 parent a7bf595 commit f96e2b7

1 file changed

Lines changed: 30 additions & 40 deletions

File tree

docs/api-guide/renderers.md

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ If your API includes views that can serve both regular webpages and API response
7171

7272
---
7373

74-
# API Reference
74+
## API Reference
7575

76-
## JSONRenderer
76+
### JSONRenderer
7777

7878
Renders the request data into `JSON`, using utf-8 encoding.
7979

@@ -96,7 +96,7 @@ The default JSON encoding style can be altered using the `UNICODE_JSON` and `COM
9696

9797
**.charset**: `None`
9898

99-
## TemplateHTMLRenderer
99+
### TemplateHTMLRenderer
100100

101101
Renders data to HTML, using Django's standard template rendering.
102102
Unlike other renderers, the data passed to the `Response` does not need to be serialized. Also, unlike other renderers, you may want to include a `template_name` argument when creating the `Response`.
@@ -141,7 +141,7 @@ See the [_HTML & Forms_ Topic Page][html-and-forms] for further examples of `Tem
141141

142142
See also: `StaticHTMLRenderer`
143143

144-
## StaticHTMLRenderer
144+
### StaticHTMLRenderer
145145

146146
A simple renderer that simply returns pre-rendered HTML. Unlike other renderers, the data passed to the response object should be a string representing the content to be returned.
147147

@@ -163,7 +163,7 @@ You can use `StaticHTMLRenderer` either to return regular HTML pages using REST
163163

164164
See also: `TemplateHTMLRenderer`
165165

166-
## BrowsableAPIRenderer
166+
### BrowsableAPIRenderer
167167

168168
Renders data into HTML for the Browsable API:
169169

@@ -187,7 +187,7 @@ By default the response content will be rendered with the highest priority rende
187187
def get_default_renderer(self, view):
188188
return JSONRenderer()
189189

190-
## AdminRenderer
190+
### AdminRenderer
191191

192192
Renders data into HTML for an admin-like display:
193193

@@ -217,7 +217,7 @@ Note that views that have nested or list serializers for their input won't work
217217

218218
**.template**: `'rest_framework/admin.html'`
219219

220-
## HTMLFormRenderer
220+
### HTMLFormRenderer
221221

222222
Renders data returned by a serializer into an HTML form. The output of this renderer does not include the enclosing `<form>` tags, a hidden CSRF input or any submit buttons.
223223

@@ -241,7 +241,7 @@ For more information see the [HTML & Forms][html-and-forms] documentation.
241241

242242
**.template**: `'rest_framework/horizontal/form.html'`
243243

244-
## MultiPartRenderer
244+
### MultiPartRenderer
245245

246246
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, using REST framework's [test client and test request factory][testing].
247247

@@ -253,31 +253,21 @@ This renderer is used for rendering HTML multipart form data. **It is not suita
253253

254254
---
255255

256-
# Custom renderers
256+
## Custom renderers
257257

258258
To implement a custom renderer, you should override `BaseRenderer`, set the `.media_type` and `.format` properties, and implement the `.render(self, data, accepted_media_type=None, renderer_context=None)` method.
259259

260260
The method should return a bytestring, which will be used as the body of the HTTP response.
261261

262262
The arguments passed to the `.render()` method are:
263263

264-
### `data`
264+
- `data`: the request data, as set by the `Response()` instantiation.
265265

266-
The request data, as set by the `Response()` instantiation.
266+
- `accepted_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"`.
267267

268-
### `accepted_media_type=None`
268+
- `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`.
269269

270-
Optional. If provided, this is the accepted media type, as determined by the content negotiation stage.
271-
272-
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"`.
273-
274-
### `renderer_context=None`
275-
276-
Optional. If provided, this is a dictionary of contextual information provided by the view.
277-
278-
By default this will include the following keys: `view`, `request`, `response`, `args`, `kwargs`.
279-
280-
## Example
270+
### Example
281271

282272
The following is an example plaintext renderer that will return a response with the `data` parameter as the content of the response.
283273

@@ -292,7 +282,7 @@ The following is an example plaintext renderer that will return a response with
292282
def render(self, data, accepted_media_type=None, renderer_context=None):
293283
return smart_str(data, encoding=self.charset)
294284

295-
## Setting the character set
285+
### Setting the character set
296286

297287
By default renderer classes are assumed to be using the `UTF-8` encoding. To use a different encoding, set the `charset` attribute on the renderer.
298288

@@ -321,7 +311,7 @@ In some cases you may also want to set the `render_style` attribute to `'binary'
321311

322312
---
323313

324-
# Advanced renderer usage
314+
## Advanced renderer usage
325315

326316
You can do some pretty flexible things using REST framework's renderers. Some examples...
327317

@@ -330,7 +320,7 @@ You can do some pretty flexible things using REST framework's renderers. Some e
330320
* Specify multiple types of HTML representation for API clients to use.
331321
* Underspecify a renderer's media type, such as using `media_type = 'image/*'`, and use the `Accept` header to vary the encoding of the response.
332322

333-
## Varying behavior by media type
323+
### Varying behavior by media type
334324

335325
In some cases you might want your view to use different serialization styles depending on the accepted media type. If you need to do this you can access `request.accepted_renderer` to determine the negotiated renderer that will be used for the response.
336326

@@ -357,7 +347,7 @@ For example:
357347
data = serializer.data
358348
return Response(data)
359349

360-
## Underspecifying the media type
350+
### Underspecifying the media type
361351

362352
In some cases you might want a renderer to serve a range of media types.
363353
In this case you can underspecify the media types it should respond to, by using a `media_type` value such as `image/*`, or `*/*`.
@@ -366,15 +356,15 @@ If you underspecify the renderer's media type, you should make sure to specify t
366356

367357
return Response(data, content_type='image/png')
368358

369-
## Designing your media types
359+
### Designing your media types
370360

371361
For the purposes of many Web APIs, simple `JSON` responses with hyperlinked relations may be sufficient. If you want to fully embrace RESTful design and [HATEOAS] you'll need to consider the design and usage of your media types in more detail.
372362

373363
In [the words of Roy Fielding][quote], "A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources and driving application state, or in defining extended relation names and/or hypertext-enabled mark-up for existing standard media types.".
374364

375365
For good examples of custom media types, see GitHub's use of a custom [application/vnd.github+json] media type, and Mike Amundsen's IANA approved [application/vnd.collection+json] JSON-based hypermedia.
376366

377-
## HTML error views
367+
### HTML error views
378368

379369
Typically a renderer will behave the same regardless of if it's dealing with a regular response, or with a response caused by an exception being raised, such as an `Http404` or `PermissionDenied` exception, or a subclass of `APIException`.
380370

@@ -391,11 +381,11 @@ Templates will render with a `RequestContext` which includes the `status_code` a
391381
!!! note
392382
If `DEBUG=True`, Django's standard traceback error page will be displayed instead of rendering the HTTP status code and text.
393383

394-
# Third party packages
384+
## Third party packages
395385

396386
The following third party packages are also available.
397387

398-
## YAML
388+
### YAML
399389

400390
[REST framework YAML][rest-framework-yaml] provides [YAML][yaml] parsing and rendering support. It was previously included directly in the REST framework package, and is now instead supported as a third-party package.
401391

@@ -416,7 +406,7 @@ Modify your REST framework settings.
416406
],
417407
}
418408

419-
## XML
409+
### XML
420410

421411
[REST Framework XML][rest-framework-xml] provides a simple informal XML format. It was previously included directly in the REST framework package, and is now instead supported as a third-party package.
422412

@@ -437,7 +427,7 @@ Modify your REST framework settings.
437427
],
438428
}
439429

440-
## JSONP
430+
### JSONP
441431

442432
[REST framework JSONP][rest-framework-jsonp] provides JSONP rendering support. It was previously included directly in the REST framework package, and is now instead supported as a third-party package.
443433

@@ -460,11 +450,11 @@ Modify your REST framework settings.
460450
],
461451
}
462452

463-
## MessagePack
453+
### MessagePack
464454

465455
[MessagePack][messagepack] is a fast, efficient binary serialization format. [Juan Riaza][juanriaza] maintains the [djangorestframework-msgpack][djangorestframework-msgpack] package which provides MessagePack renderer and parser support for REST framework.
466456

467-
## Microsoft Excel: XLSX (Binary Spreadsheet Endpoints)
457+
### Microsoft Excel: XLSX (Binary Spreadsheet Endpoints)
468458

469459
XLSX is the world's most popular binary spreadsheet format. [Tim Allen][flipperpa] of [The Wharton School][wharton] maintains [drf-excel][drf-excel], which renders an endpoint as an XLSX spreadsheet using OpenPyXL, and allows the client to download it. Spreadsheets can be styled on a per-view basis.
470460

@@ -501,23 +491,23 @@ To avoid having a file streamed without a filename (which the browser will often
501491
renderer_classes = [XLSXRenderer]
502492
filename = 'my_export.xlsx'
503493

504-
## CSV
494+
### CSV
505495

506496
Comma-separated values are a plain-text tabular data format, that can be easily imported into spreadsheet applications. [Mjumbe Poe][mjumbewu] maintains the [djangorestframework-csv][djangorestframework-csv] package which provides CSV renderer support for REST framework.
507497

508-
## UltraJSON
498+
### UltraJSON
509499

510500
[UltraJSON][ultrajson] is an optimized C JSON encoder which can give significantly faster JSON rendering. [Adam Mertz][Amertz08] maintains [drf_ujson2][drf_ujson2], a fork of the now unmaintained [drf-ujson-renderer][drf-ujson-renderer], which implements JSON rendering using the UJSON package.
511501

512-
## CamelCase JSON
502+
### CamelCase JSON
513503

514504
[djangorestframework-camel-case] provides camel case JSON renderers and parsers for REST framework. This allows serializers to use Python-style underscored field names, but be exposed in the API as Javascript-style camel case field names. It is maintained by [Vitaly Babiy][vbabiy].
515505

516-
## Pandas (CSV, Excel, PNG)
506+
### Pandas (CSV, Excel, PNG)
517507

518508
[Django REST Pandas] provides a serializer and renderers that support additional data processing and output via the [Pandas] DataFrame API. Django REST Pandas includes renderers for Pandas-style CSV files, Excel workbooks (both `.xls` and `.xlsx`), and a number of [other formats]. It is maintained by [S. Andrew Sheppard][sheppard] as part of the [wq Project][wq].
519509

520-
## LaTeX
510+
### LaTeX
521511

522512
[Rest Framework Latex] provides a renderer that outputs PDFs using Lualatex. It is maintained by [Pebble (S/F Software)][mypebble].
523513

0 commit comments

Comments
 (0)