1.安装库:

pip install drf-yasg==1.20.0

 

2.注册app:

swagger  文档django实现_接口文档

 

 

3.配置settings.py 同目录的urls.py路由:

from django.contrib import admin
from django.urls import path
from django.urls import include
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions

schema_view = get_schema_view(
openapi.Info(
title="接口文档", # 必传
default_version='v1', # 必传
description="swagger接口文档",
terms_of_service="https://drf-yasg.readthedocs.io/en/stable/openapi.html",
contact=openapi.Contact(email="chenquan2@codemao.cn"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,)
)
urlpatterns = [
path('admin/', admin.site.urls),

path('swagger-mysite/', schema_view.with_ui('swagger', cache_timeout=0),
name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schemaredoc'),
]
4.编写视图:
注意需要视图需要 GenericApivIEw以及他的派生子类

swagger  文档django实现_django_02

 

 

swagger  文档django实现_html_03

 

 

文档访问:

swagger  文档django实现_接口文档_04