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/
相关推荐
简佐义的博客7 分钟前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
爬山算法8 分钟前
MySQL(116)如何监控负载均衡状态?
数据库·mysql·负载均衡
老纪的技术唠嗑局3 小时前
OceanBase PoC 经验总结(二)—— AP 业务
数据库
阿里云大数据AI技术3 小时前
OpenSearch 视频 RAG 实践
数据库·人工智能·llm
m0_623955666 小时前
Oracle使用SQL一次性向表中插入多行数据
数据库·sql·oracle
阿蒙Amon6 小时前
C#读写文件:多种方式详解
开发语言·数据库·c#
东窗西篱梦7 小时前
Redis集群部署指南:高可用与分布式实践
数据库·redis·分布式
就是有点傻7 小时前
C#如何实现中英文快速切换
数据库·c#
1024小神8 小时前
hono框架绑定cloudflare的d1数据库操作步骤
数据库
KellenKellenHao9 小时前
MySQL数据库主从复制
数据库·mysql