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/
相关推荐
笃行客从不躺平16 分钟前
遇到大SQL怎么处理
java·开发语言·数据库·sql
逻极1 小时前
Redis Queue (RQ) 核心原理:轻量任务队列的设计与实践(一句话讲透核心本质)
数据库·redis·bootstrap
q***31831 小时前
Window下Redis的安装和部署详细图文教程(Redis的安装和可视化工具的使用)
数据库·redis·缓存
大锦终2 小时前
【MySQL】基本查询
数据库·mysql
last_zhiyin2 小时前
Oracle sql tuning guide 翻译 Part 6-5 --- Hint使用报告的操作方法和例子
数据库·sql·oracle·sql tunning
Rysxt_2 小时前
Spring Boot SPI 教程
java·数据库·sql
空影星2 小时前
高效追踪电脑使用时间,Tockler助你优化时间管理
python·django·flask
避避风港2 小时前
MySQL 从入门到实战
数据库·mysql
s***4532 小时前
MSSQL2022的一个错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.16.0”提供程序
数据库·microsoft
能鈺CMS3 小时前
能鈺CMS · 虚拟发货源码
java·大数据·数据库