Django自动生成docs接口文档

1.创建Django项目

python 复制代码
python manage.py startproject django2025

2.创建子应用

python 复制代码
python manage.py startapp api

3.安装依赖包

python 复制代码
pip install coreapi

4.创建urls.py

python 复制代码
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视图

python 复制代码
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas import AutoSchema


class MyAPIView(APIView):
    """
    A simple API View with GET and POST methods.
    """
    schema = AutoSchema()  # 自动创建API文档的Schema

    def 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.访问页面检查

python 复制代码
http://127.0.0.1:8000/docs/
相关推荐
zzh0816 分钟前
PG数据库日常应用
数据库·oracle
阿维的博客日记6 分钟前
MySQL中type字段解析
数据库·mysql
Trouvaille ~9 分钟前
【MySQL篇】表的操作:数据的容器
linux·数据库·mysql·oracle·xshell·ddl·表的操作
黑牛儿11 分钟前
从0开始实现Mysql主从配置实战
服务器·数据库·后端·mysql
爱学习的小囧17 分钟前
vSphere 9.0 API 实操教程 —— 轻松检索 vGPU 与 DirectPath 配置文件
linux·运维·服务器·网络·数据库·esxi·vmware
麦聪聊数据20 分钟前
数据库安全与运维管控(一):MySQL、PG与Oracle原生审计机制对比
运维·数据库·mysql·oracle
ZHENGZJM25 分钟前
后端基石:Go 项目初始化与数据库模型设计
开发语言·数据库·golang
毕胜客源码28 分钟前
改进yolov8的香蕉成熟度检测系统,改进前后的模型指标对比,有技术文档,支持图像、视频和摄像实时检测
人工智能·python·深度学习·yolo·django
小小程序员.¥36 分钟前
oracle--plsql块、存储过程、存储函数
数据库·sql·oracle
fire-flyer38 分钟前
ClickHouse系列(四):压缩不是为了省磁盘,而是为了更快的查询
数据库·clickhouse