You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/renderers.md
+30-40Lines changed: 30 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,9 +71,9 @@ If your API includes views that can serve both regular webpages and API response
71
71
72
72
---
73
73
74
-
# API Reference
74
+
##API Reference
75
75
76
-
## JSONRenderer
76
+
###JSONRenderer
77
77
78
78
Renders the request data into `JSON`, using utf-8 encoding.
79
79
@@ -96,7 +96,7 @@ The default JSON encoding style can be altered using the `UNICODE_JSON` and `COM
96
96
97
97
**.charset**: `None`
98
98
99
-
## TemplateHTMLRenderer
99
+
###TemplateHTMLRenderer
100
100
101
101
Renders data to HTML, using Django's standard template rendering.
102
102
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
141
141
142
142
See also: `StaticHTMLRenderer`
143
143
144
-
## StaticHTMLRenderer
144
+
###StaticHTMLRenderer
145
145
146
146
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.
147
147
@@ -163,7 +163,7 @@ You can use `StaticHTMLRenderer` either to return regular HTML pages using REST
163
163
164
164
See also: `TemplateHTMLRenderer`
165
165
166
-
## BrowsableAPIRenderer
166
+
###BrowsableAPIRenderer
167
167
168
168
Renders data into HTML for the Browsable API:
169
169
@@ -187,7 +187,7 @@ By default the response content will be rendered with the highest priority rende
187
187
def get_default_renderer(self, view):
188
188
return JSONRenderer()
189
189
190
-
## AdminRenderer
190
+
###AdminRenderer
191
191
192
192
Renders data into HTML for an admin-like display:
193
193
@@ -217,7 +217,7 @@ Note that views that have nested or list serializers for their input won't work
217
217
218
218
**.template**: `'rest_framework/admin.html'`
219
219
220
-
## HTMLFormRenderer
220
+
###HTMLFormRenderer
221
221
222
222
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.
223
223
@@ -241,7 +241,7 @@ For more information see the [HTML & Forms][html-and-forms] documentation.
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].
247
247
@@ -253,31 +253,21 @@ This renderer is used for rendering HTML multipart form data. **It is not suita
253
253
254
254
---
255
255
256
-
# Custom renderers
256
+
##Custom renderers
257
257
258
258
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.
259
259
260
260
The method should return a bytestring, which will be used as the body of the HTTP response.
261
261
262
262
The arguments passed to the `.render()` method are:
263
263
264
-
###`data`
264
+
-`data`: the request data, as set by the `Response()` instantiation.
265
265
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"`.
267
267
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`.
269
269
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
281
271
282
272
The following is an example plaintext renderer that will return a response with the `data` parameter as the content of the response.
283
273
@@ -292,7 +282,7 @@ The following is an example plaintext renderer that will return a response with
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.
298
288
@@ -321,7 +311,7 @@ In some cases you may also want to set the `render_style` attribute to `'binary'
321
311
322
312
---
323
313
324
-
# Advanced renderer usage
314
+
##Advanced renderer usage
325
315
326
316
You can do some pretty flexible things using REST framework's renderers. Some examples...
327
317
@@ -330,7 +320,7 @@ You can do some pretty flexible things using REST framework's renderers. Some e
330
320
* Specify multiple types of HTML representation for API clients to use.
331
321
* Underspecify a renderer's media type, such as using `media_type = 'image/*'`, and use the `Accept` header to vary the encoding of the response.
332
322
333
-
## Varying behavior by media type
323
+
###Varying behavior by media type
334
324
335
325
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.
336
326
@@ -357,7 +347,7 @@ For example:
357
347
data = serializer.data
358
348
return Response(data)
359
349
360
-
## Underspecifying the media type
350
+
###Underspecifying the media type
361
351
362
352
In some cases you might want a renderer to serve a range of media types.
363
353
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
366
356
367
357
return Response(data, content_type='image/png')
368
358
369
-
## Designing your media types
359
+
###Designing your media types
370
360
371
361
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.
372
362
373
363
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.".
374
364
375
365
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.
376
366
377
-
## HTML error views
367
+
###HTML error views
378
368
379
369
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`.
380
370
@@ -391,11 +381,11 @@ Templates will render with a `RequestContext` which includes the `status_code` a
391
381
!!! note
392
382
If `DEBUG=True`, Django's standard traceback error page will be displayed instead of rendering the HTTP status code and text.
393
383
394
-
# Third party packages
384
+
##Third party packages
395
385
396
386
The following third party packages are also available.
397
387
398
-
## YAML
388
+
###YAML
399
389
400
390
[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.
401
391
@@ -416,7 +406,7 @@ Modify your REST framework settings.
416
406
],
417
407
}
418
408
419
-
## XML
409
+
###XML
420
410
421
411
[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.
422
412
@@ -437,7 +427,7 @@ Modify your REST framework settings.
437
427
],
438
428
}
439
429
440
-
## JSONP
430
+
###JSONP
441
431
442
432
[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.
443
433
@@ -460,11 +450,11 @@ Modify your REST framework settings.
460
450
],
461
451
}
462
452
463
-
## MessagePack
453
+
###MessagePack
464
454
465
455
[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.
466
456
467
-
## Microsoft Excel: XLSX (Binary Spreadsheet Endpoints)
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.
470
460
@@ -501,23 +491,23 @@ To avoid having a file streamed without a filename (which the browser will often
501
491
renderer_classes = [XLSXRenderer]
502
492
filename = 'my_export.xlsx'
503
493
504
-
## CSV
494
+
###CSV
505
495
506
496
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.
507
497
508
-
## UltraJSON
498
+
###UltraJSON
509
499
510
500
[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.
511
501
512
-
## CamelCase JSON
502
+
###CamelCase JSON
513
503
514
504
[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].
515
505
516
-
## Pandas (CSV, Excel, PNG)
506
+
###Pandas (CSV, Excel, PNG)
517
507
518
508
[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].
519
509
520
-
## LaTeX
510
+
###LaTeX
521
511
522
512
[Rest Framework Latex] provides a renderer that outputs PDFs using Lualatex. It is maintained by [Pebble (S/F Software)][mypebble].
0 commit comments