当前位置: 首页 > news >正文

Django自动生成docs接口文档

1.创建Django项目

python manage.py startproject django2025

2.创建子应用

python manage.py startapp api

3.安装依赖包

pip install coreapi

4.创建urls.py

from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from api.views import MyAPIView
from rest_framework.schemas import get_schema_view
from rest_framework.documentation import include_docs_urls
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(openapi.Info(title="API Documentation",default_version='v1',),public=False
)
urlpatterns = [path('admin/', admin.site.urls),path(r'docs/', include_docs_urls(title='API文档')),path('api/hello/', MyAPIView.as_view()),
]

5.编写view.py视图

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas import AutoSchemaclass MyAPIView(APIView):"""A simple API View with GET and POST methods."""schema = AutoSchema()  # 自动创建API文档的Schemadef get(self, request, *args, **kwargs):response = {'message': 'Hello, World!'}return Response(response)def post(self, request, *args, **kwargs):response = {'message': 'Got some data!', 'data': request.data}return Response(response)

6.访问页面检查

http://127.0.0.1:8000/docs/

在这里插入图片描述

http://www.lryc.cn/news/240604.html

相关文章:

  • Mock 数据
  • (三)C语言之for语句概述
  • OpenLDAP配置web管理界面PhpLDAPAdmin服务-centos9stream
  • 深兰科技多款大模型技术产品登上新闻联播!
  • 移远通信推出六款新型天线,为物联网客户带来更丰富的产品选择
  • 八、ffmpeg录制视频为yuv文件
  • Rust并发编程:理解线程与并发
  • 二次开发问题汇总【C#】
  • 中职组网络安全B模块-渗透提权2
  • 【考研】数据结构(更新到循环链表)
  • DB2—03(DB2中常见基础操作)
  • 华为云cce健康检查有什么用?配置需要注意什么?
  • 微信小程序会议OA-登录获取手机号流程登录-小程序导入微信小程序SDK(从微信小程序和会议OA登录获取手机号到登录小程序导入微信小程序SDK)
  • 原来 TinyVue 组件库跨框架(Vue2、Vue3、React、Solid)是这样实现的?
  • 自定义label组件
  • 【Linux】使用Makefile自动化编译项目:简化开发流程、提高效率
  • 浅谈开源和闭源的认知
  • 你了解Postman 变量吗?
  • ArmSoM-RK3588编解码之mpp编码demo解析:mpi_enc_test
  • 【ES6.0】-详细模块化、export与Import详解
  • 网工内推 | Base北京,国企网工运维,最高30k*14薪,IE认证优先
  • SQL LIKE 运算符:用法、示例和通配符解释
  • 编译原理Lab1-用FLEX构造C-Minus-f词法分析器
  • 网络安全之渗透测试入门准备
  • 【MySQL】宝塔面板结合内网穿透实现公网远程访问
  • 通过AX6000路由器,实现外部访问内网的任意主机
  • 如何应用ChatGPT撰写、修改论文及工作报告,提供写作能力及优化工作??
  • camera-caps:Jetson设备上的一种实用的V4L2可视化界面
  • CAN基础知识
  • vue3跨域怎么解决?