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/
相关推荐
nvd111 小时前
深入分析:Pytest异步测试中的数据库会话事件循环问题
数据库·pytest
appearappear1 小时前
如何安全批量更新数据库某个字段
数据库
·云扬·2 小时前
MySQL 常见存储引擎详解及面试高频考点
数据库·mysql·面试
羊小猪~~2 小时前
【QT】--文件操作
前端·数据库·c++·后端·qt·qt6.3
coding-fun3 小时前
电子发票批量提取导出合并助手
大数据·数据库
leo_2323 小时前
备份&恢复--SMP(软件制作平台)语言基础知识之三十九
数据库·数据安全·开发工具·smp(软件制作平台)·应用系统
何以不说话3 小时前
mysql 的主从复制
运维·数据库·学习·mysql
二二牧人3 小时前
qemu arm64 linux开发环境搭建
linux·运维·数据库
茁壮成长的露露3 小时前
导出导入工具mongoexport、mongoimport
数据库·mongodb
Coder_Boy_3 小时前
基于SpringAI的在线考试系统-考试系统DDD(领域驱动设计)实现步骤详解
java·数据库·人工智能·spring boot