当前位置:首页 > 编程笔记 > 正文
已解决

Django自动生成docs接口文档

来自网友在路上 190890提问 提问时间:2023-11-19 14:24:46阅读次数: 90

最佳答案 问答题库908位专家为你答疑解惑

1.创建Django项目

python manage.py startproject django2025

2.创建子应用

python manage.py startapp api

3.安装依赖包

pip install coreapi

4.创建urls.py

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视图

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas import AutoSchemaclass MyAPIView(APIView):"""A simple API View with GET and POST methods."""schema = AutoSchema()  # 自动创建API文档的Schemadef 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.访问页面检查

http://127.0.0.1:8000/docs/

在这里插入图片描述

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Django自动生成docs接口文档":http://eshow365.cn/6-39352-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!