drf-yasg


drf-yasg 공식 사이트

먼저 pip를 통해 drf-yasg를 설치한다.

pip install drf-yasg

그 다음 장고 INSTALLED_APP에 다음 부분을 추가한다.

INSTALLED_APPS = [
   'drf_yasg',
]

프로젝트 내의 settings.py 에 아래와 같이 입력해준다. (Django에서 이제는 url이 아닌 path를 지원하지만 drf-yasg 공식문서에는 url을 사용해서 url을 Import 해주었습니다)

from django.conf.urls import url
from rest_framework import permissions

from drf_yasg.views import get_schema_view
from drf_yasg import openapi

...

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
      terms_of_service="<https://www.google.com/policies/terms/>",
      contact=openapi.Contact(email="[email protected]"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
   url(r'^swagger(?P<format>\\.json|\\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
   url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
   url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
   ...
]

공식 문서에 따르면 다음과 같이 총 4개의 엔드포인트가 생성된다.