1.使用coreapi自动生成api接口文档
1.1 下载coreapi,Pygments,Markdown
pip3 install coreapi
1.2 配置:
drf>3.10的,需要在全局配置文件settings.py中添加
REST_FRAMEWORK = { # 指定用于支持coreapi的Schema 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema', }
1.3 全局路由表中配置路由
1.4 前端访问自动生成的api接口文档
1.5 自定义每个action下面的描述信息
2.使用drf-yasg自动生成swagger-api接口文档
2.1 安装drf-yasg
pip3 install drf-yasg
2.2 配置到全局配置文件settings.py中的INSTALLED_APPS 列表中
2.3 在全局路由表中添加配置
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title='ChunTian API接口文档平台', # 必传
default_version='v1.0', # 必传
description='这是自动化测试平台的接口文档',
terms_of_service='http://localhost:8000',
contact=openapi.Contact(email='tianjianli_ct@163.com'),
license=openapi.License(name='BSD License'),
),
# pubic 指定访问权限
public=True,
)
2.4 在全局路由表中添加路由条目
urlpatterns = [
# 添加子路由
path('projects/', include("projects.urls")),
path('interfaces/', include("interfaces.urls")),
# 配置coreapi接口文档路由
path('docs/', include_docs_urls(title='自动化测试平台接口文档', description='xx接口文档')),
# 配置drf-yasg接口文档路由
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
path(r'swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path(r'redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
2.5 前端访问swagger-api文档