美文网首页
2022-10-23

2022-10-23

作者: 你猜猜我是谁or宇宙第一帅 | 来源:发表于2022-10-22 16:27 被阅读0次
     class ProjectDetailApiView(APIView):
        """查询单个项目、删除项目、修改项目接口"""
        def get_object(self, pk):
            try:
                return Project.objects.get(pk=pk)
            except Project.DoesNotExist:
                raise Http404
    
        def get(self, request, pk, format=None):
            project = self.get_object(pk)
            serializer = ProjectSerializer(project)
            return Response(response_data_format(1, "success", serializer.data), status=status.HTTP_200_OK)
    
        def put(self, request, pk, format=None):
            project = self.get_object(pk)
            serializer = ProjectSerializer(instance=project, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(response_data_format(1, "success", serializer.data), status=status.HTTP_200_OK)
            return Response(response_data_format(0, "fail", serializer.data), status=status.HTTP_200_OK)
    
        def delete(self, request, pk, format=None):
            project = self.get_object(pk)
            project.delete()
            return Response(response_data_format(0, "fail", None), status=status.HTTP_204_NO_CONTENT)
    
    
    
    url(r'^projects/$', views.ProjectApiView.as_view()),
    url(r'^projects/(?P<pk>[0-9]+)$', views.ProjectDetailApiView.as_view()),
    

    相关文章

      网友评论

          本文标题:2022-10-23

          本文链接:https://www.haomeiwen.com/subject/fgiizrtx.html