dajngo的接口文档自动生成
代码实现
文档一:redoc
- 模块导入并在配置文件中添加相关配置
pip install drf-yasg
pip install django
urls.py
中导入
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
urls.py
中添加
# 配置信息
schema_view = get_schema_view(
openapi.Info(
title="Django-Rest-Framework API", #接口文档名称
default_version='v4', # 接口文档版本
contact=openapi.Contact(email="3441292862@qq.com"), #开发者邮箱地址
license=openapi.License(name="MIT License"), #许可证
),
public=True, #是否公开
permission_classes=[],
)
urlpatterns = [
"""
其他接口信息
"""
path('', schema_view.with_ui('redoc', cache_timeout=0), name='redoc'),
]
- 示例
文档二:swagger-UI
- 模块导入
pip install drf-yasg
pip install django
urls.py
中导入
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
urls.py
中添加
# 配置信息
schema_view = get_schema_view(
openapi.Info(
title="Django-Rest-Framework API", #接口文档名称
default_version='v4', # 接口文档版本
contact=openapi.Contact(email="3441292862@qq.com"), #开发者邮箱地址
license=openapi.License(name="MIT License"), #许可证
),
public=True, #是否公开
permission_classes=[],
)
urlpatterns = [
"""
其他接口信息
"""
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
- 示例
文档三:rest_framework
的docs
- 模块导入
pip install django
pip install coreapi-cli
pip install djangorestframework
urls.py
中导入
from rest_framework.documentation import include_docs_urls
项目的配置文件中添加以下代码
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}
urls.py
中添加
urlpatterns = [
"""
其他接口信息
"""
path('docs/', include_docs_urls(title="接口文档", authentication_classes=[], permission_classes=[])),
]
- 示例