Django Rest swagger Generate API documentation

Source: Internet
Author: User
Tags install django pip install django

About Swagger

Swagger can be one of the most popular rest APIs documentation generation tools for the following reasons:

    • Swagger can generate an interactive API console that developers can use to quickly learn and experiment with the API.
    • Swagger can generate client SDK code for implementations on a variety of different platforms.
    • Swagger files can be automatically generated from code annotations on many different platforms.
    • Swagger has a strong community with many powerful contributors.

The following is the actual live Django rest swagger for DRF API interface documentation

Environment
    • Python3.6
    • Django1.11
    • Django-rest-swagger
    • Djangorestframework
Installation
pip install django==1.11.6pip instal djangorestframeworkpip install django-rest-swagger
Create projects and apps
startproject apiteststartapp api
Configure 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,}
Configure api/serializer.py
# 序列化from django.contrib.auth.models import User,Groupfrom  rest_framework import serializersclass UserSerializer(serializers.HyperlinkedModelSerializer):    class Meta:        model = User        fields = "__all__"class GroupSerializer(serializers.HyperlinkedModelSerializer):    class Meta:        model =Group        fields = "__all__"
Configure api/views.py
# 视图from  django.contrib.auth.models import User,Groupfrom rest_framework import viewsetsfrom  api.serializers import UserSerializer,GroupSerializer# Create your views here.class UserViewSet(viewsets.ModelViewSet):    '''查看,编辑用户的界面'''    queryset = User.objects.all().order_by('-date_joined')    serializer_class = UserSerializerclass GroupViewSet(viewsets.ModelViewSet):    '''查看,编辑组的界面'''    queryset = Group    serializer_class = GroupSerializer
Configure apitest/urls.py
from django.conf.urls import url,includefrom django.contrib import adminfrom  rest_framework import routersfrom  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_viewfrom rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRendererschema_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'))]
The final effect DRF the interface UI that comes with it

Swagger UI

Django Rest swagger Generate API documentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.