第十四单元 视图集路由

视图plus

from django.shortcuts import render
from myapp.models import Province,Student
from rest_framework.views import APIView
from myapp.serializers import ProvinceSerializers,StudentSerializers,StudentSerializers2
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
# Create your views here.
class ProvinceView(ModelViewSet):
    queryset = Province.objects.all()
    serializer_class = ProvinceSerializers

class StudentView(ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializers

class StudentView2(APIView):
    def get(self,request,pk):
        try:
            stu_data = Student.objects.filter(province_id=pk)
        except Exception as e:
            print(e)
            return Response({'msg':'error'},status=404)
        ser = StudentSerializers2(stu_data,many=True)
        return Response(ser.data,status=200)

路由plus

from django.urls import path
from myapp import views
from rest_framework import routers
urlpatterns = [
    path('student2/<int:pk>/',views.StudentView2.as_view())
]

# 生成路由对象
router = routers.SimpleRouter()
# 使用路由对象生成路由信息
router.register('province',views.ProvinceView,'province')
router.register('student',views.StudentView,'student')
# 路由追加
urlpatterns +=router.urls
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值