How can i generate static api documentation for django rest APIs?

Viewed 315

I went through the documentation and tried to use swagger and coreapi, but both are interactive, I want the same api documentation without interaction. Is it possible?

Here you can see API documentation

https://editor.swagger.io/

but its interacting with the respective api. I want same UI and without interaction.

1 Answers

Oh it is really easy.

  1. First install: django-rest-swagger==2.1.2
  2. Add to settings: 'rest_framework_swagger'

Next make urls.py changes:

from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework import permissions
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(
    title='Swagger My - API documentation',
    public=True,
    renderer_classes=[CoreJSONRenderer, OpenAPIRenderer, SwaggerUIRenderer],
    authentication_classes=[JSONWebTokenAuthentication],
    permission_classes=[permissions.AllowAny]
)

urlpatterns = [
    # Swagger
    path('docs/', schema_view),
]

And you choose renderer_classes=[CoreJSONRenderer, OpenAPIRenderer, SwaggerUIRenderer].

This is example with JWT JSONWebTokenAuthentication for auth.

Related