...

프로그래밍/Django(장고) + 웹 +11
반응형

원래는 django_rest_swagger를 사용했었는데 더 이상 업데이트가 안되고 에러가 나서

 

drf-yasg로 바꿨습니다

 

 

- 먼저 drf-yasg를 설치 합니다

pip install drf-yasg

 

- settings.py 에 설치한 앱을 추가 해줍니다

INSTALLED_APPS = [
    ...
    'drf_yasg',
]

 

-  urls.py 에 아래의 내용을 추가 해줍니다

...
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from django.urls import path, include
from django.conf.urls import url
from rest_framework.permissions import AllowAny


schema_view_v1 = get_schema_view(
    openapi.Info(
        title="Open API",
        default_version='v1',
        description="간단한 설명",
        terms_of_service="https://www.google.com/policies/terms/",
    ),
    public=True,
    permission_classes=(AllowAny,),
)


urlpatterns = [
	...

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

 

- 아래의 주소로 접속하면 API 문서를 볼 수 있습니다

http://127.0.0.1:8000/redoc/
http://127.0.0.1:8000/swagger/
반응형