Swagger是一个很好用的管理Api文档的工具,不仅仅Spring系列有自动化生成Swagger Api文档的工具包,Python同样也有(配置非常非常简单)!
废话不多说,直接上代码!!!
- 用到的包
使用drf-yasg这个第三方工具包来完成。
pip install drf-yasg - 配置setting
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sers',
'rest_framework',
'drf_yasg' <-----配置部分
]
- url,py
# 配置swagger各个参数
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
schema_view = get_schema_view(
openapi.Info(
title="Django-DRF API", <----名称
default_version="v1.0.0", <-----版本
description="Welcome to the world of Django-DRF", <----项目描述
),
public=True,
)
urlpatterns = [
# 这两个url配置是一定要有的,用于生成ui界面,其它url正常定义就好
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
- 访问Api
启动项目
使用http://127.0.0.1:8000/swagger/访问 - 效果
【文章编写不易,如需转发请联系作者!】