0030 apidoc接口文档

2021-01-08 22:30

阅读:566

标签:site   插件   app   编译   nucleus   word   ram   pat   object   

1 创建Django工程

 

1.1 安装pip,django等相关插件

1.2 创建app

1.3 创建视图

 

from django.shortcuts import render, HttpResponse
from django.views.generic import View


# Create your views here.
class Login(View):
    @classmethod
    def get(cls, request):
        return HttpResponse(‘ok‘)

 

1.4 配置路由

from django.contrib import admin
from django.urls import path
from nucleus.views import Login

urlpatterns = [
    path(‘admin/‘, admin.site.urls),
    path(‘Login/‘, Login.as_view()),
]

 

2 安装apidoc

  使用淘宝镜像安装

2.1 命令

npm config set registry https://registry.npm.taobao.org

1.2 验证命令

npm config get registry

  如果返回https://registry.npm.taobao.org,说明镜像配置成功。

2 安装apidoc

npm install -g apidoc -g --registry=https://registry.npm.taobao.org

3 为视图编写注释代码

 

from django.shortcuts import render, HttpResponse
from django.views.generic import View


# Create your views here.
class Login(View):
    @classmethod
    def get(cls, request):
        """
        @api {GET} /Login/登录操作
        @apiVersion 0.0.1
        @apiName Login
        @apiGroup User
        @apiDescription 这里可以描述一下这个函数的具体操作
        这一行也可以描述的
        @apiParam {String} name 姓名
        @apiParam {String} password 密码
        @apiSuccess {Object} status 状态码
        @apiSuccess {Object} msg 简略描述
        @apiSuccessExample Response-Success:
        HTTP 1.1/ 200K
            {
                ‘status‘: 0,
                ‘msg‘: ‘success‘
            }
        @apiErrorExample Response-Fail:
            HTTP 1.1/ 404K
            {
                ‘status‘: 1,
                ‘msg‘: ‘Fail‘
            }
        """
        return HttpResponse(‘ok‘)

 

 

4 先在项目根目录下放一个apidoc.json文件,示例如下:

{  
  "name": "测试",  
  "version": "0.0.1",  
  "description": "API文档测试",  
  "title": "API文档测试",  
  "url" : "http://xxxxxxx",  
  "sampleUrl" : "http://xxxxxxxx",  
  "template":{  
    "forceLanguage":"zh_cn"  
  }  
} 

5 运行生产文档

  每次更新都必须重新编译才能够访问到最新的注释。

apidoc -i nucleus/ -o static/apidoc # nucleus是APP目录

6 配置settings.py

  在项目根目录下创建static/apidoc

STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)
APIDOC_ROOT = os.path.join(STATIC_ROOT, ‘apidoc‘)

7 配置url

from django.contrib import admin
from django.urls import path, re_path
from nucleus.views import Login
from django.views.static import serve
from django.conf import settings

urlpatterns = [
    path(‘admin/‘, admin.site.urls),
    path(‘Login/‘, Login.as_view()),
    re_path(r‘^apidoc/(?P.*)$‘, serve, {‘document_root‘: settings.APIDOC_ROOT}),
]

8 浏览器访问

http://127.0.0.1:8000/apidoc/index.htm

 

 

  

 

0030 apidoc接口文档

标签:site   插件   app   编译   nucleus   word   ram   pat   object   

原文地址:https://www.cnblogs.com/dorian/p/12634642.html


评论


亲,登录后才可以留言!