通用类视图简化了自己的代码。但是我觉得会让人觉得不清晰。因为hide details.
这里调用gcbv/list/会报错:
views.py文件内容:
from django.http.response import HttpResponse from rest_framework.decorators import api_view from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from .models import Course from .serializers import CourseSerializer from rest_framework import generics """一、函数式编程""" @api_view(["GET","POST"]) def course_list(request): """ 获取所有课程信息或新增一个课程 """ if request.method == "GET": s = CourseSerializer(instance=Course.objects.all(),many=True) return Response(data=s.data, status=status.HTTP_200_OK) elif request.method == "POST": s = CourseSerializer(data=request.data, partial=True) if s.is_valid(): s.save(teacher=request.user) return Response(data=s.data, status=status.HTTP_201_CREATED) return Response(s.errors, status=status.HTTP_400_BAD_REQUEST) @api_view(["GET","PUT","DELETE"]) def course_detail(request,pk): """ 获取、删除更新一个课程 """ try: course = Course.objects.get(pk=pk) except Course.DoesNotExist: return Response(data={"msg": "没有此课程信息"},status=status.HTTP_404_NOT_FOUND) else: if request.method == "GET": s = CourseSerializer(instance=course) return Response(data=s.data, status=status.HTTP_200_OK) elif request.method == "PUT": # 这里的instance的只是查出来的course,而data则是request传来的值 s = CourseSerializer(instance=course, data=request.data) if s.is_valid(): s.save() return Response(s.data,status=status.HTTP_200_OK) elif request.method == "DELETE": course.delete() return Response(status=status.HTTP_204_NO_CONTENT) """类试图 Class Based Views""" class CourseList(APIView): def get(self, request): queryset = Course.objects.all() s = CourseSerializer(instance=queryset, many=True) # instance = xxx 是查询集 return Response(s.data, status=status.HTTP_200_OK) def post(self,request): s = CourseSerializer(data=request.data) # data是传递过来的数据,return前要先调用is_valid()方法验证数据 if s.is_valid(): s.save(teacher=self.request.user) return Response(data=s.data, status=status.HTTP_201_CREATED) return Response(s.errors, status.HTTP_400_BAD_REQUEST) class CourseDetail(APIView): @staticmethod def get_object(pk): try: return Course.objects.get(pk=pk) except Course.DoesNotExist: # 返回none return def get(self,request,pk): obj = self.get_object(pk) if not obj: return Response(data={"msg": "没有此课程信息"}, status=status.HTTP_404_NOT_FOUND) s = CourseSerializer(instance=obj) return Response(s.data, status=status.HTTP_200_OK) def put(self,request,pk): obj = self.get_object(pk) if not obj: return Response(data={"msg": "没有此课程信息"}, status=status.HTTP_404_NOT_FOUND) s = CourseSerializer(instance=obj, data=request.data) if s.is_valid(): s.save() return Response(data=s.data, status=status.HTTP_201_CREATED) return Response(data=s.data,status=status.HTTP_400_BAD_REQUEST) def delete(self,request,pk): obj = self.get_object(pk) if not obj: return Response(data={"msg": "没有此课程信息"}, status=status.HTTP_404_NOT_FOUND) obj.delete() return Response(status=status.HTTP_204_NO_CONTENT) """三、通用类试图 Generic Class Based View""" class GCourseList(generics.ListCreateAPIView): queryset = Course.objects.all() serializer_class = CourseSerializer def perform_create(self, serializer): serializer.save(teacher=self.request.user) class GCourseDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Course.objects.all() serializer_class = CourseSerializer