swagger 文档django实现
原创
©著作权归作者所有:来自51CTO博客作者wx5b6d6c951e615的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.安装库:
pip install drf-yasg==1.20.0
2.注册app:
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以及他的派生子类
文档访问: