Django_swagger格式接口文档(django的)

安装和配置

1.安装

pip install drf-yasg

2.settings.py 文件中注册第三方应用

INSTALLED_APPS = [

   *****
     "django.contrib.staticfiles",  # swagger用到的静态资源在这里
    # swagger接口文档
    'drf_yasg',
   
   *****
]

3.urls.py中添加路径

#swagger接口文档
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from django.urls import path,include,re_path

schema_view = get_schema_view(
    openapi.Info(
        title="接口平台API",
        default_version='v1.0',
        description="接口平台接口文档",
        terms_of_service="#",
        contact=openapi.Contact(email="测试"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    # permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    
    *****
     # 配置drf-yasg路由
    # 访问路径  /swagger.json  好像没啥用

    re_path('^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),

    # 互动模式访问路径
    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'),

    *******
]

4.使用        

给接口加注释 

视图集(list,create,update,destrory, retrieve)  和 视图类的加法如下

from django.utils.decorators import  method_decorator
from drf_yasg.utils import swagger_auto_schema

@method_decorator(name='get',decorator=swagger_auto_schema(operation_summary="获取项目列表"))
@method_decorator(name='post',decorator=swagger_auto_schema(operation_summary="新增项目"))
class InterfacesView(APIView):
    ....

效果

给函数视图的加法是这么加的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成 Django API 接口文档一般可以通过以下步骤实现: 1. 安装 Django-rest-swagger 可以通过 pip 安装 Django-rest-swagger: ``` pip install django-rest-swagger ``` 2. 添加 Django-rest-swagger 到 INSTALLED_APPS 在 settings.py 文件中添加 Django-rest-swagger 到 INSTALLED_APPS: ```python INSTALLED_APPS = [ ... 'rest_framework', 'rest_framework_swagger', ... ] ``` 3. 添加 Django-rest-swagger 的 URL 到项目的 URL 配置文件 在项目的 URL 配置文件(例如 urls.py)中添加 Django-rest-swagger 的 URL: ```python from rest_framework_swagger.views import get_swagger_view schema_view = get_swagger_view(title='API 文档') urlpatterns = [ ... url(r'^docs/$', schema_view), ... ] ``` 4. 添加 API 文档注释 在 API 视图函数中,添加类似以下的注释,用于生成 API 文档: ```python from rest_framework.decorators import api_view, schema from rest_framework.response import Response from rest_framework.schemas import AutoSchema @api_view(['GET']) @schema(AutoSchema(manual_fields=[ coreapi.Field(name="param1", required=True, location='query', schema=coreschema.Integer()), coreapi.Field(name="param2", required=True, location='query', schema=coreschema.Integer()), ])) def my_api_view(request): """ API endpoint documentation example. --- parameters: - name: param1 description: Parameter description required: true type: integer paramType: query - name: param2 description: Parameter description required: true type: integer paramType: query """ # Implementation return Response(...) ``` 5. 运行项目 运行项目后,访问 http://localhost:8000/docs/ 即可看到生成的 API 文档。 以上步骤是生成 Django API 接口文档的一种方法,也可以使用其他的工具或框架来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值