• Django Rest Swagger生成api文档


    1.安装

    pip install django-rest-swagger

    2.配置 rest_api/settings.py

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # 加入以下
        'api',
        'rest_framework',
        'rest_framework_swagger',
    
    ]
    # swagger 配置项
    SWAGGER_SETTINGS = {
        # 基础样式
        'SECURITY_DEFINITIONS': {
            "basic":{
                'type': 'basic'
            }
        },
        # 如果需要登录才能够查看接口文档, 登录的链接使用restframework自带的.
        'LOGIN_URL': 'rest_framework:login',
        'LOGOUT_URL': 'rest_framework:logout',
        # 'DOC_EXPANSION': None,
        # 'SHOW_REQUEST_HEADERS':True,
        # 'USE_SESSION_AUTH': True,
        # 'DOC_EXPANSION': 'list',
        # 接口文档中方法列表以首字母升序排列
        'APIS_SORTER': 'alpha',
        # 如果支持json提交, 则接口文档中包含json输入框
        'JSON_EDITOR': True,
        # 方法列表字母排序
        'OPERATIONS_SORTER': 'alpha',
        'VALIDATOR_URL': None,
    }

    3.配置apitest/urls.py

    from django.conf.urls import url,include
    from django.contrib import admin
    from  rest_framework import routers
    from  api import views
    
    # 路由
    router = routers.DefaultRouter()
    router.register(r'users',views.UserViewSet,base_name='user')
    router.register(r'groups',views.GroupViewSet,base_name='group')
    
    
    # 重要的是如下三行
    from rest_framework.schemas import get_schema_view
    from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
    schema_view = get_schema_view(title='Users API', renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])
    
    
    
    urlpatterns = [
        # swagger接口文档路由
        url(r'^docs/', schema_view, name="docs"),
        url(r'^admin/', admin.site.urls),
        url(r'^',include(router.urls)),
        # drf登录
        url(r'^api-auth/',include('rest_framework.urls',namespace='rest_framework'))
    
    ]
  • 相关阅读:
    2013-2014-2(复变函数56)
    2015-2016-1(实变函数56)
    2014-2015-2(常微分方程64, 数学分析提高64)
    2015-2016-2(点集拓扑56, 点集拓扑56)
    2016-2017-2(点集拓扑56, 点集拓扑56)
    2016-2017-1(实变函数56, 点集拓扑56)
    2017-2018-1(实变函数56, 点集拓扑56)
    2013-2014 指导2014届毕业生8名(48)
    2015-2016 指导2016届毕业生8名(48)
    2014-2015 指导2015届毕业生8名(48)
  • 原文地址:https://www.cnblogs.com/kcxg/p/12600371.html
Copyright © 2020-2023  润新知