[英]DELETE method in Django Rest
I am trying to DELETE record in Django Rest. 我正在尝试在Django Rest中删除记录。
views.py :- views.py:-
class ItemPartialView(generics.RetrieveUpdateDestroyAPIView):
queryset = itemlist.objects.all()
serializer_class = ItemlistSerializer
def put(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
def destroy(self, request, *args, **kwargs):
obj = self.get_object()
if obj.survey:
return Response(data={'message': "Too late to delete"},
status=status.HTTP_400_BAD_REQUEST)
self.perform_destroy(obj)
return Response(status=status.HTTP_204_NO_CONTENT)
urls.py :- urls.py:-
urlpatterns = {
url(r'^itemlists/$', ItemView.as_view(), name="create"),
url(r'^itemlists/(?P<pk>\d+)/$', ItemPartialUpdateView.as_view(), name="update")
}
Now, when I am sending DELETE request to itemlists/1/
, it is not deleting the record with id = 1
Error is showing Method DELETE not allowed
现在,当我将DELETE请求发送到itemlists/1/
,它没有删除id = 1
的记录。错误显示Method DELETE not allowed
(CREATE, READ, UPDATE are working except DELETE, so I don't think it's cors related issue). (除了DELETE之外,CREATE,READ,UPDATE都有效,因此我认为这与cors相关)。
You seem to have 2 routes defined for itemlists/1/
. 您似乎为itemlists/1/
定义了2条路线。 Your code is hitting the first one which only has partial update (PUT/PATCH) support judging from the name. 从名称来看,您的代码已达到第一个仅具有部分更新(PUT / PATCH)支持的代码。
You should create one view with both update and destroy on the same class. 您应该在同一个类上创建一个同时具有更新和销毁视图的视图。 That should work. 那应该工作。
As I see it - you don't use your ItemPartialView
in your urls. 如我所见-您不会在URL中使用ItemPartialView
。
You use ItemPartialUpdateView
instead, so my guess is that delete is not supported with this view. 您改为使用ItemPartialUpdateView
,所以我的猜测是此视图不支持删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.