Django Admin 还有 browser API 是方便浏览操作API, 但是不太适合全局阅读,而Swagger UI 是公认的API 文档说明最好的,
只需要花费两分钟就可以配置好,这里我们不使用django-rest-swagger,这个作者已经弃用多年,我们使用drf-yasg2
官方文档上推荐使用drf-yasg,但是它不能兼容最新的DRF,所以我们使用drf-yasg2
pip install drf-yasg2
更新项目文件里的 settings.py 来加载 drf_yasg2
-
-
-
-
-
-
'django.contrib.contenttypes',
-
'django.contrib.sessions',
-
'django.contrib.messages',
-
'django.contrib.staticfiles',
-
-
-
-
-
更新项目里的 urls.py 文件来加载 the schema_view
-
-
-
from rest_framework import permissions
-
from drf_yasg2.views import get_schema_view
-
from drf_yasg2 import openapi
-
schema_view = get_schema_view(
-
-
-
-
description="Welcome to the world of Tweet",
-
-
contact=openapi.Contact(email="demo@tweet.org"),
-
license=openapi.License(name="Awesome IP"),
-
-
-
permission_classes=(permissions.AllowAny,),
-
-
-
-
-
-
-
-
re_path(r'^doc(?P\.json|\.yaml)$',schema_view.without_ui(cache_timeout=0), name='schema-json'),
-
path('doc/', 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'),
-
path(r'polls/', include('polls.urls')),
-
path(r'tweet/', include('tweets.urls')),
-
path(r'admin/', admin.site.urls),
-
path(r'api-auth/', include(('rest_framework.urls', 'rest_framework'), namespace="api-auth"))
-
运行项目:
redoc:
配置完成
阅读(1252) | 评论(0) | 转发(0) |