文章目錄
coreapi接口文檔
pip install coreapi
pip install markdown
# settings.py
# DRF配置資訊
REST_FRAMEWORK = {
# >3.10版本的DRF需要此項配置
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}
# urls.py
from rest_framework.documentation import include_docs_urls
urlpatterns = [
...
path('docs/', include_docs_urls(title='測試平台接口文檔', description='這是一個接口測試平台'))
]
http://127.0.0.1:8000/docs/
swagger接口文檔
pip install drf-yasg
# settings.py
# 增加應用
INSTALLED_APPS = ['drf_yasg']
# urls.py
from django.urls import path, re_path
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="Lemon API接口文檔平台", # 必傳
default_version='v1', # 必傳
description="這是一個美輪美奂的接口文檔",
terms_of_service="http://api.keyou.site",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="BSD License"),
),
public=True,
# permission_classes=(permissions.AllowAny,), # 權限類
)
urlpatterns = [
# json或yaml格式
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
# swagger
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
# redoc
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
http://127.0.0.1:8000/swagger.json
http://127.0.0.1:8000/swagger
http://127.0.0.1:8000/redoc