Skip to content

Instantly share code, notes, and snippets.

@dotja
Last active October 25, 2024 15:59
Show Gist options
  • Save dotja/6da4783e3f1f1e1dd569a116660e9c42 to your computer and use it in GitHub Desktop.
Save dotja/6da4783e3f1f1e1dd569a116660e9c42 to your computer and use it in GitHub Desktop.
Django REST Framework API documentation using Swagger

API Documentation with DRF and Swagger

Overview:

  • We need to create a schema. The schema outlines all the endpoints and actions of our API.

  • We need to create the documentation that is a more human-readable form of the schema.

Steps:

Generate schema

  1. install pyyaml and uritemplate
  2. edit the top level urls.py file to include:
from rest_framework.schemas import get_schema_view

urlpatterns = [
    # ...
    path('api_schema/', get_schema_view(
        title='API Schema',
        description='Guide for the REST API'
    ), name='api_schema'),
    # ...
]

The schema will now be found at http://http//127.0.0.1:8000/api_schema

Generate documentation

  1. install Django Swagger module:

pip install django-rest-swagger

  1. add it to to INSTALLED_APPS and update the TEMPLATES section

  2. create the HTML template file eg. docs.html in templates/

<!DOCTYPE html>
<html>
  <head>
    <title>School Service Documentation</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
  </head>
  <body>
    <div id="swagger-ui"></div>
    <script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
    <script>
    const ui = SwaggerUIBundle({
        url: "{% url schema_url %}",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIBundle.SwaggerUIStandalonePreset
        ],
        layout: "BaseLayout"
      })
    </script>
  </body>
</html>
  1. update urls.py to include swagger doc:
from django.views.generic import TemplateView

urlpatterns = [
    path('docs/', TemplateView.as_view(
        template_name='docs.html',
        extra_context={'schema_url':'api_schema'}
        ), name='swagger-ui'),
]

API docs will now be found at http://127.0.0.1:8000/swagger-ui

@Nelio-Bila
Copy link

THANK YOU, it work like a charm

@nasirovx
Copy link

thanks bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment