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/
相关推荐
落叶的悲哀42 分钟前
mysql tidb like查询有换行符内容问题解决
数据库·mysql·tidb
wangchen_01 小时前
MySQL索引
数据库·mysql
哈__1 小时前
数据库迁移实操与金仓数据库技术优势:从语法兼容到自动化落地
数据库
蟹至之1 小时前
增删查改(其一) —— insert插入 与 select条件查询
数据库·mysql·增删查改
Yeats_Liao2 小时前
时序数据库系列(七):性能监控实战指标收集
数据库·后端·时序数据库
无心水2 小时前
【中间件:Redis】1、Redis面试核心:线程模型深度解析(6.0前后变化+工作流程)
数据库·redis·面试·redis面试·redis原理·redis线程模型·后端技术
milanyangbo2 小时前
从同步耦合到异步解耦:消息中间件如何重塑系统间的通信范式?
java·数据库·后端·缓存·中间件·架构
狮子也疯狂2 小时前
基于Django实现的智慧校园考试系统-自动组卷算法实现
python·算法·django
绛洞花主敏明3 小时前
Gorm(十四)的多条件叠加
数据库
枫叶丹43 小时前
【Qt开发】布局管理器(五)-> QSpacerItem 控件
开发语言·数据库·c++·qt