All Projects → twtrubiks → django_rest_framework_swagger_tutorial

twtrubiks / django_rest_framework_swagger_tutorial

Licence: other
django rest framework swagger tutorial

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to django rest framework swagger tutorial

Pretty Swag
Pretty UI for Swagger spec
Stars: ✭ 112 (+460%)
Mutual labels:  swagger, api-documentation
Redoc
📘 OpenAPI/Swagger-generated API Reference Documentation
Stars: ✭ 15,935 (+79575%)
Mutual labels:  swagger, api-documentation
Angular Swagger Ui
An angularJS implementation of Swagger UI
Stars: ✭ 131 (+555%)
Mutual labels:  swagger, api-documentation
Spring Boot Webflux Swagger Starter
An example project to illustrate how to document Spring Boot Webflux with Swagger2
Stars: ✭ 62 (+210%)
Mutual labels:  swagger, api-documentation
Drf Autodocs
Ultimately automated DRF documentation rendering(UNMAINTAINED)
Stars: ✭ 82 (+310%)
Mutual labels:  django-rest-framework, api-documentation
Openapi Viewer
Browse and test a REST API described with the OpenAPI 3.0 Specification
Stars: ✭ 82 (+310%)
Mutual labels:  swagger, api-documentation
Api Development Tools
📚 A collection of useful resources for building RESTful HTTP+JSON APIs.
Stars: ✭ 2,519 (+12495%)
Mutual labels:  swagger, api-documentation
Awesome Documentation Tools
🔥 📚 All the tools, processes and resources you need to create an awesome API & Project documentation
Stars: ✭ 138 (+590%)
Mutual labels:  swagger, api-documentation
Train Ai With Django Swagger Jwt
Train AI (Keras + Tensorflow) to defend apps with Django REST Framework + Celery + Swagger + JWT - deploys to Kubernetes and OpenShift Container Platform
Stars: ✭ 66 (+230%)
Mutual labels:  django-rest-framework, swagger
Flasgger
Easy OpenAPI specs and Swagger UI for your Flask API
Stars: ✭ 2,825 (+14025%)
Mutual labels:  swagger, api-documentation
Molten Boilerplate
A boilerplate for the molten framework by Bogdanp https://github.com/Bogdanp/molten
Stars: ✭ 50 (+150%)
Mutual labels:  swagger, api-documentation
Drf Yasg
Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
Stars: ✭ 2,523 (+12515%)
Mutual labels:  django-rest-framework, swagger
Create Openapi Repo
🤖 Generator for GH repo to help you manage the OpenAPI definition lifecycle
Stars: ✭ 513 (+2465%)
Mutual labels:  swagger, api-documentation
Swagger Github Pages
How to host Swagger API documentation with GitHub Pages
Stars: ✭ 102 (+410%)
Mutual labels:  swagger, api-documentation
Hexo Theme Doc
A documentation theme for the Hexo blog framework
Stars: ✭ 222 (+1010%)
Mutual labels:  swagger, api-documentation
Django Rest Swagger Docs
Beginners approach to Django Rest Swagger
Stars: ✭ 86 (+330%)
Mutual labels:  django-rest-framework, swagger
network-pipeline
Network traffic data pipeline for real-time predictions and building datasets for deep neural networks
Stars: ✭ 36 (+80%)
Mutual labels:  django-rest-framework, swagger
nestjs-auth-starter-kit
NestJS Auth Starter Kit (typescript / typeorm / swagger / passport / bcrypt)
Stars: ✭ 37 (+85%)
Mutual labels:  swagger
django-restful-admin
Django admin restful api
Stars: ✭ 51 (+155%)
Mutual labels:  django-rest-framework
eoLinker
在线 API 研发管理测试工具,最后能用的开源修复版本(4.0.1本地测试插件兼容3.5与4.0版本)。
Stars: ✭ 62 (+210%)
Mutual labels:  api-documentation

django-rest-framework-swagger-tutorial

Django-REST-Swagger 基本教學

相信大家在網路上一定都看過 API 文件

那我們該如何撰寫 API 文件 給別人看呢 ?

今天我要教大家使用 Swagger 來完成他 !!

溫馨小提醒

建議大家先對 Django 以及 Django REST framework ( DRF ) 有基礎的知識。

如果還不熟的人,可以先閱讀我之前寫的

Django 基本教學 - 從無到有 Django-Beginners-Guide

以及

Django-REST-framework 基本教學 - 從無到有 DRF-Beginners-Guide

先建立一些基本觀念,再來看這篇比較清楚。

教學

我們依照 Django-REST-framework 基本教學 - 從無到有 DRF-Beginners-Guide 這篇繼續延伸下去。

請在你的命令提示字元 (cmd ) 底下輸入

安裝 Django-rest-swagger

pip install django-rest-swagger

django-rest-swagger 設定

請記得要將 Django-rest-swagger 加入設定檔

請在 settings.py 裡面的 INSTALLED_APPS 加入下方程式碼

INSTALLED_APPS = (
    ...
    'rest_framework_swagger',
    ...
)

如果加入上方程式碼,目前 settings.py 裡面的 INSTALLED_APPS 應該會變成這樣 ( 如下圖 )

alt tag

接著我們設定 Routers 路由 ,請將 urls.py 增加一些程式碼

from django.conf.urls import url
from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='API')

urlpatterns = [
    url(r'^$', schema_view)
]

如果加入上方程式碼,目前的 urls.py 會變成這樣 ( 如下圖 )

alt tag

32 行我們先把他註解掉,

最後執行 Django , 然後瀏覽 http://127.0.0.1:8000/docs/

你應該會看到如下圖 ( 如果你沒看到任何東西,可以點一下 Show/Hide )

alt tag

執行畫面

畫面非常漂亮,功能也非常完善,

我們可以在上面執行 GET , POST , PUT , PATCH , DELETE , 甚至可以直接看到執行結果,

我介紹幾個給大家當範例,看完大家就會比較了解

POST

點選編號 1 的地方,他會幫你把範例貼到左手邊,修改完值之後,直接按 Try it out!

接著你會發現下面有 Response , 以這個範例來講,201 就是新增成功

GET

接著我們再去 GET ,我們剛剛新增的那筆的確有在裡面

有沒有發現非常強大 😮

接下來你可能會擔心,這樣我的資料不就會被任何人任意操作 ? 不用擔心,和之前介紹的 授權 (Authentication ) 是一樣的。

授權( Authentication )

urls.py 底下加入下方程式碼 ( 也就是剛剛註解掉的程式碼 )

urlpatterns = [
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

views.py 底下加入 permission

# Create your views here.
class MusicViewSet(viewsets.ModelViewSet):
    queryset = Music.objects.all()
    serializer_class = MusicSerializer
    permission_classes = (IsAuthenticated,)

settings.py 底下加入下方程式碼

LOGIN_URL = 'rest_framework:login'
LOGOUT_URL = 'rest_framework:logout'

執行 Django , 然後瀏覽 http://127.0.0.1:8000/docs/

你會發現,當你沒有登入的時候,你是看不到這些 API 的內容

alt tag

登入之後你才有權限可以看到這些資料

我的 帳號/密碼 設定為 twtrubiks/password123 ,

Swagger 的基本介紹我們就介紹到這邊,更多的說明可以參考 Django-rest-swagger

結論

雖然 Django-rest-swagger 非常強大,但有時候你會發現他自訂性比較低,

所以說可能還是要考慮當下的需求下去選擇撰寫 API 文件 的工具,

如果你需要自訂性較高撰寫 API 文件 的工具,

可以參考 aglio_tutorial

執行環境

  • Python 3.5.3

Reference

License

MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].