Loading

drf 生成接口文档

生成接口文档

21.1 coreapi的使用

安装依赖

pip install coreapi

配置

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',  # django原生接口
    'app01',  # drf接口
    'sers',  # 序列化器的使用
    'req',  # drf提供的请求与响应
    'viewdemo',  # 视图
    'school',  # 序列化器的嵌套
    'opt',  # drf组件
    'django_filters',  # 过滤
    'coreapi',  # 生成接口文档
    'rest_framework',
]

REST_FRAMEWORK = {
	......
    # 接口文档生成
    'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.AutoSchema',
}

路由

from django.contrib import admin
from django.urls import path, include
from rest_framework.documentation import include_docs_urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/',include('app.urls')),
    path('api/', include('app01.urls')),
    path('sers/', include('sers.urls')),
    path('req/', include('req.urls')),
    path('viewdemo/', include('viewdemo.urls')),
    path('school/', include('school.urls')),
    path('opt/', include('opt.urls')),
    path('docs/', include_docs_urls(title="drf教程api接口文档")),
]

访问接口文档

GET 127.0.0.1:8000/docs/

x2Tnpt.png

21.2 yasg的使用

安装依赖

pip install drf-yasg

配置

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',  # django原生接口
    'app01',  # drf接口
    'sers',  # 序列化器的使用
    'req',  # drf提供的请求与响应
    'viewdemo',  # 视图
    'school',  # 序列化器的嵌套
    'opt',  # drf组件
    'django_filters',  # 过滤
    'coreapi',  # 生成接口文档
    'drf_yasg',  # 生成接口文档
    'rest_framework',
]

路由

import rest_framework.permissions
from django.contrib import admin
from django.urls import path, include
from rest_framework.documentation import include_docs_urls

# yasg的视图配置类,用于生成api
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
    openapi.Info(
        title="drf教程api文档",  # 必须
        default_version="v1.0.0",  # 必须
        description="drf学习时写的案例,现在生成一个接口文档",
        terms_of_service='',
        contact=openapi.Contact(email="3049399358@qq.com"),
        license=openapi.License(name="闵麒良"),
    ),
    # public=True,  # 所有人访问
    permission_classes=[rest_framework.permissions.IsAuthenticated],  # 权限类,和上面的互斥
)
urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('app.urls')),
    path('api/', include('app01.urls')),
    path('sers/', include('sers.urls')),
    path('req/', include('req.urls')),
    path('viewdemo/', include('viewdemo.urls')),
    path('school/', include('school.urls')),
    path('opt/', include('opt.urls')),
    path('docs/', include_docs_urls(title="drf教程api接口文档")), # coreapi
    path('doc/', schema_view.with_ui("swagger",cache_timeout=0),name="schema-swagger"), # swagger
]

访问接口文档

GET 127.0.0.1:8000/doc/

x2TK6f.png

posted @ 2022-10-24 17:38  minqiliang  阅读(119)  评论(0编辑  收藏  举报
-->