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/
相关推荐
APItesterCris3 分钟前
构建弹性数据管道:利用淘宝商品 API 进行流式数据采集与处理
linux·数据库·windows
九河云9 分钟前
TOS + 数字孪生:集装箱码头的智能进化密码
大数据·服务器·网络·数据库·数字化转型
手握风云-36 分钟前
MySQL数据库精研之旅第十九期:存储过程,数据处理的全能工具箱(二)
数据库·mysql
孟意昶38 分钟前
Doris专题17- 数据导入-文件格式
大数据·数据库·分布式·sql·doris
你可以永远相信功夫熊猫1 小时前
金蝶云·星瀚 | 生产制造成本核算终极实操手册(从0到1,含两套完整案例)
数据库·erp
Thepatterraining1 小时前
MySQL零基础教程:DDL/DCL/DML详解,从建库到存储过程一篇搞定!
数据库·sql·mysql
想ai抽1 小时前
深入starrocks-怎样实现多列联合统计信息
java·数据库·数据仓库
jackletter1 小时前
待补充 五大关系数据库(sqlserver、mysql、oracle、pgsql、sqlite)的列类型:目录
mysql·oracle·sqlserver·sqlite·pgsql·列类型
Y4090012 小时前
MySQL中的“事务”
数据库·mysql
Raymond运维2 小时前
MySQL源码编译安装
linux·数据库·mysql