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/
相关推荐
阿杆5 小时前
如何在 Spring Boot 中接入 Amazon ElastiCache
java·数据库·redis
qq_343247035 小时前
单机版认证kafka
数据库·分布式·kafka
2301_800256115 小时前
第十一章 PostgreSQL 服务器编程知识点梳理(1)
服务器·数据库·postgresql
松涛和鸣5 小时前
DAY32 Linux Thread Programming
linux·运维·数据库·算法·list
秦jh_5 小时前
【Qt】常用控件(上)
服务器·数据库·qt
爬山算法6 小时前
Netty(14)如何处理Netty中的异常和错误?
java·前端·数据库
꧁坚持很酷꧂6 小时前
把虚拟机Ubuntu中的USB设备名称改为固定名称
linux·数据库·ubuntu
1024肥宅6 小时前
浏览器存储 API:全面解析与高级实践
前端·数据库·浏览器
Evan芙6 小时前
Nginx 平滑升级
数据库·nginx·ubuntu
亚林瓜子7 小时前
mysql命令行手动导入csv数据到指定表
数据库·mysql·gui·csv·cli·db·import