1.安装drf-yasg2
pip install drf-yasg2
2.更新项目文件里的 settings.py 来加载 drf_yasg2
3.更新项目里的 urls.py 文件来加载 the schema_view
from rest_framework import permissions
from drf_yasg2.views import get_schema_view
from drf_yasg2 import openapi
schema_view = get_schema_view(
openapi.Info(
title="Python API",
default_version='v1',
description="Welcome to the world of Tweet",
),
public=True,
permission_classes=(permissions.AllowAny,),
)
re_path(r'^doc(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'), # 导出
path('doc/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), # <-- 美化UI
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), # <-- 这里