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/
相关推荐
小Tomkk10 分钟前
StarRocks SRCA 考试心得总结
数据库·数据库 starrocks·srca
千千寰宇19 分钟前
[数据库/SQL] 浅谈DDL、DSL、DCL、DML、DQL
数据库
noravinsc44 分钟前
django admin 去掉新增 删除
python·django·sqlite
爱编程的小新☆1 小时前
【MySQL】数据类型和表的操作
java·数据库·mysql
聪明的墨菲特i2 小时前
SQL进阶知识:七、数据库设计
数据库·sql·mysql·oracle·db2·数据库设计·范式
APItesterCris3 小时前
Flutter 移动端开发:集成淘宝 API 实现商品数据实时展示 APP
大数据·数据库·flutter
极小狐4 小时前
极狐GitLab 议题权重有什么作用?
开发语言·数据库·chrome·c#·gitlab
懵逼的小黑子5 小时前
解释两个 Django 命令 makemigrations和migrate
数据库·django
Lxinccode7 小时前
Java查询数据库表信息导出Word-获取数据库实现[1]:KingbaseES
java·数据库·word·获取数据库信息·获取kingbasees信息
豆沙沙包?8 小时前
5.学习笔记-SpringMVC(P61-P70)
数据库·笔记·学习