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/
相关推荐
TDengine (老段)1 小时前
TDengine Python 连接器入门指南
大数据·数据库·python·物联网·时序数据库·tdengine·涛思数据
萧曵 丶1 小时前
事务ACID特性详解
数据库·事务·acid
kejiayuan2 小时前
CTE更易懂的SQL风格
数据库·sql
kaico20182 小时前
MySQL的索引
数据库·mysql
清水白石0083 小时前
解构异步编程的两种哲学:从 asyncio 到 Trio,理解 Nursery 的魔力
运维·服务器·数据库·python
资生算法程序员_畅想家_剑魔3 小时前
Mysql常见报错解决分享-01-Invalid escape character in string.
数据库·mysql
PyHaVolask3 小时前
SQL注入漏洞原理
数据库·sql
ptc学习者3 小时前
黑格尔时代后崩解的辩证法
数据库
代码游侠3 小时前
应用——智能配电箱监控系统
linux·服务器·数据库·笔记·算法·sqlite
!chen4 小时前
EF Core自定义映射PostgreSQL原生函数
数据库·postgresql