栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Django中的swagger文档

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django中的swagger文档

Swagger UI 是公认的API 文档说明最好的,

只需要花费两分钟就可以配置好,在这里我们使用drf-yasg2

首先需要下载:pip install drf-yasg2

然后需要在setting里配置

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'one',
    'corsheaders',
    'rest_framework',
    'news',
    'django_crontab',
    'drf_yasg2',  #<----这里
]

还需要更新urls.py文件

# drf_yasg2 从这里开始
from rest_framework import permissions
from drf_yasg2.views import get_schema_view
from drf_yasg2 import openapi
schema_view = get_schema_view(
    openapi.Info(
        title="Tweet API",
        default_version='v1',
        description="Welcome to the world of Tweet",
        terms_of_service="https://www.tweet.org",
        contact=openapi.Contact(email="demo@tweet.org"),
        license=openapi.License(name="Awesome IP"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)
# 这里结束

下面路由的配置

from django.contrib import admin
from django.urls import path,include,re_path
from one import url
from news import urls
urlpatterns = [
    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'),  #<-- 这里
]

运行看页面效果

 还有一个

 之后,我们要在views里用到它

from drf_yasg2.utils import swagger_auto_schema
from drf_yasg2 import openapi
from rest_framework.decorators import action

这里是简单的一个演示

class UpdateGoods(APIView):
    @swagger_auto_schema(method="post",request_body=openapi.Schema(
        type=openapi.TYPE_OBJECT,
        required=['id','name','price'],properties=
        {"id":openapi.Schema(type=openapi.TYPE_INTEGER,description="商品id"),
         "name":openapi.Schema(type=openapi.TYPE_STRING,description="商品名称"),
         "price":openapi.Schema(type=openapi.TYPE_INTEGER,description="商品价格"),
         "lock_stock":openapi.Schema(type=openapi.TYPE_INTEGER,description="锁定库存")}
        ),)
    @action(methods=['post'],detail=False,)
    def post(self,request):
        try:
            data= request.data
            id = data['id']
            name = data['name']
            price = data['price']
            lock_stock = data['lock_stock']
            Goods.objects.filter(id=id).update(name=name,price=price,lock_stock=lock_stock)
            return Response({"code":200, "messag":"ok"})
        except:
            error = traceback.format_exc()
            print("error",error)
            return Response({"code":400,"message":"错误"})

这样我们就可以在页面上进行操作了

 测试

 

 希望对大家有所帮助,谢谢!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977948.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号