环境准备

  环境要求:

  python3

  django2

  pip3

  模块安装:

  pip3 install django-rest-framework

  pip3 install django-rest-swagger

  搭建项目:

  搭建django项目,创建testapi app

参数配置

  setting.py: 

  INSTALLED_APPS中添加:rest_framework,rest_framework_swagger

  

视图编辑

  编辑views.py

  

 # Create your views here.
# -*- coding: utf-8 -*- from rest_framework.views import APIView from rest_framework.permissions import AllowAny
from rest_framework.schemas import SchemaGenerator
from rest_framework.schemas.generators import LinkNode, insert_into
from rest_framework.renderers import *
from rest_framework_swagger import renderers
from rest_framework.response import Response # from rest_framework.schemas import SchemaGenerator
class MySchemaGenerator(SchemaGenerator): def get_links(self, request=None):
# from rest_framework.schemas.generators import LinkNode,
links = LinkNode() paths = []
view_endpoints = []
for path, method, callback in self.endpoints:
view = self.create_view(callback, method, request)
path = self.coerce_path(path, method, view)
paths.append(path)
view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included
if not paths:
return None
prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints:
if not self.has_view_permissions(path, method, view):
continue
link = view.schema.get_link(path, method, base_url=self.url)
# 添加下面这一行方便在views编写过程中自定义参数.
link._fields += self.get_core_fields(view) subpath = path[len(prefix):]
keys = self.get_keys(subpath, method, view) # from rest_framework.schemas.generators import LinkNode, insert_into
insert_into(links, keys, link) return links # 从类中取出我们自定义的参数, 交给swagger 以生成接口文档.
def get_core_fields(self, view):
return getattr(view, 'coreapi_fields', ()) def DocParam(name="default", location="query", required=True, description=None, type="string", *args, **kwargs):
return coreapi.Field(name=name, location=location, required=required, description=description, type=type) class ReturnJson(APIView):
coreapi_fields = ( #用于swagger doc显示方法必须字符串
DocParam("name", description='test'),
DocParam("nalanxiao", required=False, description='rohero'),
)
def get(self, request, *args, **kwargs):
json_data = {'name': 'post', 'id': 0}
return Response(json_data) def post(self, request, *args, **kwargs):
json_data = {'name': 'post', 'id': 0}
return Response(json_data)

路由设置

  编辑urls.py

  

 from django.conf.urls import url
from .views import SwaggerSchemaView, ReturnJson, StudentsApiView urlpatterns = [
url(r'^api/$', ReturnJson.as_view(), name='api'),
url(r'^api/v1/$', StudentsApiView.as_view(), name='api_v1'),
url(r'^docs/', SwaggerSchemaView.as_view(), name='apiDocs'),
]

效果展示:

  

github:

  https://github.com/Roherolxh/opstest

  觉得有帮助望给个小星星

最新文章

  1. 使用Executor管理线程
  2. js 布尔值作为开关判断
  3. LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  4. linux基础-第十单元 系统的初始化和服务
  5. 【androidstudio】将eclipse的项目导入android studio
  6. angular问题总结与反思
  7. 每天学点GDB 11
  8. ios中addtarget
  9. 通用表表达式(Common Table Expression)
  10. jQuery实例-简单选项卡-【一些常见方法(2)-练习】
  11. 客户端技术:Cookie 服务端技术:HttpSession
  12. windows可以登录qq,但无法打开浏览器页面
  13. Android切换页面效果的实现二:WebView+ViewPager
  14. nginx记录响应与POST请求日志
  15. 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
  16. asp.net core新特性(1):TagHelper
  17. JDBC操作数据库之查询数据
  18. MySQL学习4 - 数据类型一
  19. 爬取伯乐在线文章(五)itemloader
  20. 从零开始搭建django前后端分离项目 系列一(技术选型)

热门文章

  1. K大数查询(bzoj 3110)
  2. SPOJ 4060 A game with probability
  3. 标准C程序设计七---14
  4. ci框架——文章查看之上篇下篇
  5. Idea其他设置
  6. 23. 客户默认选项(Default Customer Options)
  7. SqlServer 数据恢复
  8. BUPT复试专题—串查找(?)
  9. Our happy ending
  10. poj(1011)——Sticks(经典的dfs+剪枝)