Django REST框架:使用def update()在ViewSet中不允许使用方法PUT [英] Django REST framework: method PUT not allowed in ViewSet with def update()
问题描述
在DRF中,我有一个像这样的简单ViewSet:
In DRF, I have a simple ViewSet like this one:
class MyViewSet(viewsets.ViewSet):
def update(self, request):
# do things...
return Response(status=status.HTTP_200_OK)
尝试PUT请求时,出现类似方法PUT的错误。如果我使用 def put(self,request):
一切正常。根据文档我应该使用 def update():
而不是 def put():
,为什么会发生?
When I try a PUT request, I get an error like method PUT not allowed. If I use def put(self, request):
all things work fine. Accordingly to the docs I should use def update():
not def put():
, why does it happen?
推荐答案
这是因为 APIView
没有为<$ c定义处理程序$ c> .put()方法,因此传入的请求无法映射到视图上的处理程序方法,从而引发异常。
This is because the APIView
has no handler defined for .put()
method so the incoming request could not be mapped to a handler method on the view, thereby raising an exception.
(注意: viewsets.ViewSet
继承自 ViewSetMixin
和 APIView
)
APIView中的
检查是否为请求 dispatch()
方法方法
定义了方法处理程序。如果 dispatch()
方法找到请求方法的处理程序,它返回适当的响应。否则,它会引发异常 MethodNotAllowed
。
The dispatch()
method in the APIView
checks if a method handler is defined for the request method
.If the dispatch()
method finds a handler for the request method, it returns the appropriate response. Otherwise, it raises an exception MethodNotAllowed
.
根据 dispatch()
方法的源代码, code> APIView 类:
As per the source code of dispatch()
method in the APIView
class:
def dispatch(self, request, *args, **kwargs):
...
...
try:
self.initial(request, *args, **kwargs)
# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
# here handler is fetched for the request method
# `http_method_not_allowed` handler is assigned if no handler was found
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
response = handler(request, *args, **kwargs) # handler is called here
except Exception as exc:
response = self.handle_exception(exc)
self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response
由于在您的视图中未定义 .put()
方法处理程序,因此DRF调用后备处理程序 .http_method_not_allowed
。这会引发 MethodNotAllowed
异常。
Since .put()
method handler is not defined in your view, DRF calls the fallback handler .http_method_not_allowed
. This raises an MethodNotAllowed
exception.
.http_method_not_allowed()的源代码
是:
def http_method_not_allowed(self, request, *args, **kwargs):
"""
If `request.method` does not correspond to a handler method,
determine what kind of exception to raise.
"""
raise exceptions.MethodNotAllowed(request.method) # raise an exception
定义 .put()
您认为吗?
Why it worked when you defined .put()
in your view?
定义 def put(self,request ):
在您的视图中,DRF可以将传入的请求方法映射到视图上的处理程序方法。这导致返回了适当的响应而没有引发异常。
When you defined def put(self, request):
in your view, DRF could map the incoming request method to a handler method on the view. This led to appropriate response being returned without an exception being raised.
这篇关于Django REST框架:使用def update()在ViewSet中不允许使用方法PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!