Skip to content

Commit 1d3a3ee

Browse files
committed
Add Swagger UI & ReDoc examples to topic page.
1 parent 6142aba commit 1d3a3ee

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

docs/topics/documenting-your-api.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,114 @@ There are also a number of great third-party documentation packages available.
1111

1212
## Generating documentation from OpenAPI schemas
1313

14-
TODO: Add examples for Swagger UI & ReDoc.
14+
There are a number of packages available that allow you to generate HTML
15+
documenation pages from OpenAPI schemas.
16+
17+
Two popular options are [Swagger UI][swagger-ui] and [ReDoc][redoc].
18+
19+
Both require little more than the location of your static schema file or
20+
dynamic `SchemaView` endpoint.
21+
22+
### A mimimal example with Swagger UI
23+
24+
Assuming you've followed the example from the schemas documentation for routing
25+
a dynamic `SchemaView`, a mimimal Django template for using Swagger UI might be
26+
this:
27+
28+
```html
29+
<!DOCTYPE html>
30+
<html>
31+
<head>
32+
<title>Swagger</title>
33+
<meta charset="utf-8"/>
34+
<meta name="viewport" content="width=device-width, initial-scale=1">
35+
<link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
36+
</head>
37+
<body>
38+
<div id="swagger-ui"></div>
39+
<script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
40+
<script>
41+
const ui = SwaggerUIBundle({
42+
url: "{% url schema_url %}",
43+
dom_id: '#swagger-ui',
44+
presets: [
45+
SwaggerUIBundle.presets.apis,
46+
SwaggerUIBundle.SwaggerUIStandalonePreset
47+
],
48+
layout: "BaseLayout"
49+
})
50+
</script>
51+
</body>
52+
</html>
53+
```
54+
55+
Save this in your templates folder as `swagger-ui.html`. Then route a
56+
`TemplateView` in your project's URL conf:
57+
58+
```python
59+
from django.views.generic import TemplateView
60+
61+
urlpatterns = [
62+
# ...
63+
# Route TemplateView to serve Swagger UI template.
64+
# * Provide `extra_context` with view name of `SchemaView`.
65+
path('swagger-ui/', TemplateView.as_view(
66+
template_name='swagger-ui.html',
67+
extra_context={'schema_url':'openapi-schema'}
68+
), name='swagger-ui'),
69+
]
70+
```
71+
72+
See the [Swagger UI documentation][swagger-ui] for advanced usage.
73+
74+
### A mimimal example with ReDoc.
75+
76+
Assuming you've followed the example from the schemas documentation for routing
77+
a dynamic `SchemaView`, a mimimal Django template for using Swagger UI might be
78+
this:
79+
80+
```html
81+
<!DOCTYPE html>
82+
<html>
83+
<head>
84+
<title>ReDoc</title>
85+
<!-- needed for adaptive design -->
86+
<meta charset="utf-8"/>
87+
<meta name="viewport" content="width=device-width, initial-scale=1">
88+
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
89+
<!-- ReDoc doesn't change outer page styles -->
90+
<style>
91+
body {
92+
margin: 0;
93+
padding: 0;
94+
}
95+
</style>
96+
</head>
97+
<body>
98+
<redoc spec-url='{% url schema_url %}'></redoc>
99+
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
100+
</body>
101+
</html>
102+
```
103+
104+
Save this in your templates folder as `redoc.html`. Then route a `TemplateView`
105+
in your project's URL conf:
106+
107+
```python
108+
from django.views.generic import TemplateView
109+
110+
urlpatterns = [
111+
# ...
112+
# Route TemplateView to serve the ReDoc template.
113+
# * Provide `extra_context` with view name of `SchemaView`.
114+
path('redoc/', TemplateView.as_view(
115+
template_name='redoc.html',
116+
extra_context={'schema_url':'openapi-schema'}
117+
), name='redoc'),
118+
]
119+
```
120+
121+
See the [ReDoc documentation][redoc] for advanced usage.
15122

16123
## Third party packages
17124

@@ -162,3 +269,6 @@ To implement a hypermedia API you'll need to decide on an appropriate media type
162269
[schemas-examples]: ../api-guide/schemas/#examples
163270
[metadata-docs]: ../api-guide/metadata/
164271

272+
[swagger-ui]: https://swagger.io/tools/swagger-ui/
273+
[redoc]: https://github.com/Rebilly/ReDoc
274+

0 commit comments

Comments
 (0)