$ pip install django-apidocs在 settings.py , INSTALLED_APPS 添加 django_docs
INSTALLED_APPS = [
'...',
'django_docs'
]在项目根 urls.py 中, 添加文档路由
url(r'^django_docs/', include('django_docs.urls')),启动项目, 访问 ip:port/django_docs/login/ , 输入任意用户名密码登录
新建目录与文件 api/article.py
使用原始的 View 与 JsonResponse
from django_docs import docs_define
from django.views import View
from django.http import JsonResponse
class ArticleList(View):
@docs_define('/article/list', desc='文章列表')
def get(self, request):
data = [
{
'title': 'django-docs',
'author': 'lyon',
'content': 'This is a Django application. Help you build Web API quickly.'
},
{
'title': 'django-docs-desc',
'author': 'lyon',
'content': 'Efficient, Simple and Flexible.'
}
]
return JsonResponse(data, safe=False)使用 BaseHandler
from django_docs import docs_define, BaseHandler
class ArticleList(BaseHandler):
@docs_define('/article/list', desc='文章列表')
def get(self, request):
data = [
{
'title': 'django-docs',
'author': 'lyon',
'content': 'This is a Django application. Help you build Web API quickly.'
},
{
'title': 'django-docs-desc',
'author': 'lyon',
'content': 'Efficient, Simple and Flexible.'
}
]
return self.write(data)注册 article.py
# settings.py
INSTALLED_HANDLERS = [
'api.article',
]刷新文档