规范自动生成api文档代码
在前面一篇文章,已经能够构造一个api文档了。在这里要纠正之前的一个错误观点。
错误观点:然而按照之前的方式,一个.py文件只能写一个api文档的内容。
观点来源:前面的错误观点来自于,作者在同一个.py文件中,发现不同同时插入两个 """xxxx """
类似这样的注释api信息。
正 解:一个.py文件可以写多个api文档的内容
方 法:只需要创建多个class View,在每个class中插入一个api注释代码即可。
这种使用class View的方式,也能使代码更加规范,更容易维护。
# modules/views.py
import request
from django.shortcuts import render
from django.views.generic.base import View
from common.apiResponse import ApiJsonResponse
from common.code import CODE
class IndexShowView(View):
"""
@api {post} /modules/indexShow 获取modules列表信息
@apiVersion 0.3.0
@apiName 获取查询的模块信息
@apiGroup Modules
@apiParam {String} [name=''] 需查询的模块名
@apiParam {Number} [project_id] 需查询的模块所属的项目ID
@apiParam {Number} [page=1] 页码
@apiParam {Number} [page_size=10] 每页容量
@apiParamExample {json} Request-Example:
{
'name':'网盘',
'project_id':1,
'page':1,
'page_size':10
}
@apiSuccess (success 200) {Number} code 结果码
@apiSuccess (success 200) {String} msg 结果信息
@apiSuccess (success 200) {json} data 查询返回结果
@apiSuccess (success 200) {List} list 查询结果
@apiSuccess (success 200) {Number} id 模块ID
@apiSuccess (success 200) {String} name 模块名称
@apiSuccess (success 200) {String} testers 测试人员
@apiSuccess (success 200) {String} developer 开发人员
@apiSuccess (success 200) {Number} project_id 所属项目ID
@apiSuccess (success 200) {String} desc 模块描述
@apiSuccess (success 200) {Boolean} status 状态
@apiSuccess (success 200) {Boolean} is_delete 模块是否已删除
@apiSuccess (success 200) {Datetime} create_time 项目创建时间
@apiSuccess (success 200) {Datetime} modify_time 项目修改时间
@apiSuccess (success 200) {String} project_name 所属项目名称
@apiSuccess (success 200) {Number} count 返回的模块数
@apiSuccessExample {json} Success-Response:
HTTP/1.1 200 OK
{
"code":0,
"msg":"成功",
"data":{
"list": [{
"id": 2,
"name": "网盘",
"testers": "wjr",
"developer": "wjr",
"project_id": 1,
"desc": "网盘测试",
"status": true,
"is_delete": false,
"create_time": "2019-10-25 09:42:03",
"modify_time": "2019-10-29 15:25:08",
"project_name": "sunny"
}],
"count":1
}
}
"""
def post(self,request):
modulesList=[]
modulesInfo={"id": 2,
"name": "网盘",
"testers": "wjr",
"developer": "wjr",
"project_id": 1,
"desc": "网盘测试",
"status": True,
"is_delete": False,
"create_time": "2019-10-25 09:42:03",
"modify_time": "2019-10-29 15:25:08",
"project_name": "sunny"}
modulesList.append(modulesInfo)
res={'list':modulesList,'count':1}
return ApiJsonResponse(0,CODE['0'],res)
def get(self,request):
pass
# modules/urls.py
from django.conf.urls import include,url
from apps.modules.views import IndexShowView
app_name = 'modules'
urlpatterns = [
url(r'^indexShow$', IndexShowView.as_view(), name='indexShow'),
]
为了解决403报错: