dajngo的接口文档自动生成
代码实现
文档一:redoc
- 模块导入并在配置文件中添加相关配置
pip install drf-yasgpip install django
urls.py中导入
from drf_yasg import openapifrom 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-yasgpip install django
urls.py中导入
from drf_yasg import openapifrom 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 djangopip install coreapi-clipip 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=[])),]
- 示例

