Swagger能成爲最受歡迎的REST APIs文檔生成工具之一,有如下幾個緣由:django
下面介紹Swagger生成API文檔的具體步驟:app
1、安裝 django-rest-swagger,進入虛擬環境進行安裝ide
pip instal djangorestframework pip install django-rest-swagger
2、配置settings.py函數
INSTALLED_APPS = ( .... 'rest_framework', 'rest_framework_swagger', ..... )
3、views.py裏的配置工具
class AdModelRulesViewSet(component_viewsets.ModelViewSet): ''' retrieve: Return a group instance. list: Return all groups,ordered by most recent joined. create: Create a new group. destroy: Remove a existing group. partial_update: Update one or more fields on a existing group. update: Update a group. ''' queryset = AdModelRules.objects.filter(is_delete=0) serializer_class = AdModelRulesSerializer permission_classes = (IsAdmin,)
4、urls.py裏的配置學習
# 導入輔助函數 from rest_framework.schemas import get_schema_view from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
schema_view = get_schema_view(title='API',
renderer_classes=[SwaggerUIRenderer, OpenAPIRenderer])
urlpatterns = patterns(
'home_application.views',
(r'^$', 'home'),
(r'^dev-guide/$', 'dev_guide'),
(r'^contactus/$', 'contactus'),
url(r'^', include(router.urls)),
url(r'^docs/$', schema_view, name='docs')
)
5、訪問:127.0.0.1/docs/ui
點擊‘Try it do’,便可獲取相應的文檔url