Web API接口

django-rest-framework(主打接口序列化):https://www.django-rest-framework.org/

 drf_yasg(主打接口ui): https://drf-yasg.readthedocs.io/en/stable/readme.html#usage

 

django-rest-swagger:

urls.py

from rest_framework.schemas import get_schema_view

schema_view = get_schema_view(title='Users API', renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])

urlpatterns = [
    # 身份认证
    url(r'api-auth/', include('rest_framework.urls', namespace='rest_framework')),

    # swagger API
    url(r'^docs/', schema_view, name="docs"),
]
urlpatterns = format_suffix_patterns(urlpatterns)

settings.py

INSTALLED_APPS = [
    # 接口
    'api',
    'rest_framework',
    'rest_framework_swagger',
]
# 接口--django—rest-framework
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,
}

 

 drf_yasg

settings.py

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
}
SWAGGER_SETTINGS = {
    'LOGIN_URL': '/login',
    'LOGOUT_URL': '/permissions/logout/',
    "VALIDATOR_URL": None,
    'PERSIST_AUTH': True,
    'REFETCH_SCHEMA_WITH_AUTH': True,
    'REFETCH_SCHEMA_ON_LOGOUT': True,

    'DEFAULT_INFO': 'DjangoDrfTest.urls.swagger_info',  # 这里注意,更改DjangoDrfTest

    'SECURITY_DEFINITIONS': {
        'Basic': {
            'type': 'basic'
        },
        'Bearer': {
            'type': 'apiKey',
            'name': 'authorization',
            'in': 'header'
        },
        'Query': {
            'type': 'apiKey',
            'name': 'auth',
            'in': 'query'
        }
    }
}

urls.py

from django.conf.urls import url, include
# from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from . import views
from django.contrib.auth.decorators import login_required

schema_view = get_schema_view(
    openapi.Info(
        title="智达云平台 API",
        default_version='v1',
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@snippets.local"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)
urlpatterns = [

    # swagger API
    url(r'swagger(?P<format>\.json|\.yaml)$', login_required(schema_view.without_ui(cache_timeout=0)), name='schema-json'),
    url(r'swagger/$', login_required(schema_view.with_ui('swagger', cache_timeout=0)), name='schema-swagger-ui'),
    url(r'docs/$', login_required(schema_view.with_ui('redoc', cache_timeout=0)), name='schema-redoc'),
]
# urlpatterns = format_suffix_patterns(urlpatterns)

 

posted @ 2019-01-15 12:59  逐梦客!  阅读(342)  评论(0)    收藏  举报