如何重写APIView的“post”方法?

2024-09-27 23:20:43 发布

您现在位置:Python中文网/ 问答频道 /正文

如何重写APIView的post方法?在

我试图像bellow一样重写post方法,但似乎它遗漏了一些东西。在

class CloudServerShutdownAPIView(APIView):
    """
    shut down the server
    """
    serializer_class = CloudServerShutdownSerializer
    def post(self, request):
        cloudserver_id = request.data.pop("cloudserver_id")
        try:
            openstackServerShutdown(server_or_id=cloudserver_id)
        except Exception as e:
            return Response(data="shut down server fail", status=HTTP_404_NOT_FOUND, exception=e)

如何正确重写post方法?在


编辑

回溯如下:

^{pr2}$

Tags: the方法iddataserverrequestpostclass
1条回答
网友
1楼 · 发布于 2024-09-27 23:20:43
class CloudServerShutdownAPIView(APIView):
    """
    shut down the server
    """
    serializer_class = CloudServerShutdownSerializer
    def post(self, request):
        cloudserver_id = request.data.pop("cloudserver_id")
        try:
            openstackServerShutdown(server_or_id=cloudserver_id)
            return Response(data="shut down server successful", status=HTTP_200_OK)
        except Exception as e:
            return Response(data="shut down server fail", status=HTTP_404_NOT_FOUND, exception=e)

这是因为在成功关闭期间没有发送任何响应

相关问题 更多 >

    热门问题