Loading

使用DRF实现五个api接口

使用DRF实现五个api接口

2.1 新建app

django-admin startapp app01

2.2 配置环境

# 注册app01和rest_framework,由于drf提供了web调试界面,所以静态文件功能不能注释掉,不然web界面没有样式
INSTALLED_APPS = [
    # 'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    # 'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
    'app01',
    'rest_framework',
]

2.3 视图

from app.models import *
from .serializer import studentSerializer
from rest_framework.viewsets import ModelViewSet


class studentModelViewset(ModelViewSet):
    queryset = studentsInfo.objects.all()
    serializer_class = studentSerializer

2.4 序列化器

app01/serializer.py:

from rest_framework import serializers
from app.models import *


class studentSerializer(serializers.ModelSerializer):

    class Meta:
        model = studentsInfo
        fields = "__all__"

2.5 配置路由

app01/urls.py:

from rest_framework.routers import DefaultRouter
from . import views

router = DefaultRouter()
# 以后有几个视图集就注册几个
router.register("stu", views.studentModelViewset, basename="stu")

urlpatterns = [

] + router.urls

drfdemo1/urls.py:

from django.urls import path, include

urlpatterns = [
    # path('admin/', admin.site.urls),
    path('api/',include('app.urls')),
    path('api/', include('app01.urls')),
]

2.6启动项目

python manage.py runserver 8000

2.7 测试api接口

2.7.1 获取所有数据

GET /api/stu/

返回数据

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": 1,
        "name": "小明",
        "sex": "男",
        "age": 22,
        "classroom": "301",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 2,
        "name": "小红",
        "sex": "女",
        "age": 18,
        "classroom": "301",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 3,
        "name": "张三",
        "sex": "男",
        "age": 25,
        "classroom": "307",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 4,
        "name": "李四",
        "sex": "男",
        "age": 24,
        "classroom": "308",
        "info": "这个人很懒,什么也没有留下"
    },
    {
        "id": 5,
        "name": "闵麒良",
        "sex": "男",
        "age": 23,
        "classroom": "310",
        "info": "河工胡歌"
    },
    {
        "id": 12,
        "name": "孙明辉",
        "sex": "男",
        "age": 23,
        "classroom": "333",
        "info": "这个人很懒,什么都没有留下"
    }
]

2.7.2 添加一条数据

x2oaee.png

返回数据:

HTTP 201 Created
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 14,
    "name": "啾啾",
    "sex": "女",
    "age": 22,
    "classroom": "321",
    "info": "啾啾啾啾啾啾啾啾啾啾"
}

数据库

x2owod.png

2.7.3 获取一条数据

GET /api/stu/14/

返回数据:

HTTP 200 OK
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 14,
    "name": "啾啾",
    "sex": "女",
    "age": 22,
    "classroom": "321",
    "info": "啾啾啾啾啾啾啾啾啾啾"
}

2.7.4 修改一条数据

x2orWt.png

返回数据:

HTTP 200 OK
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 14,
    "name": "呆呆",
    "sex": "男",
    "age": 20,
    "classroom": "405",
    "info": "呆呆很赖,什么都没有留下"
}

数据库:

x2ocy8.png

2.7.5 删除一条数据

x2oReg.png

DELETE /api/stu/14/

返回数据:

HTTP 204 No Content
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

数据库:

x2oWwQ.png

posted @ 2022-10-24 17:07  minqiliang  阅读(137)  评论(0编辑  收藏  举报
-->