How to delete an object using Django Rest Framework

I'm trying to write a RESTful API for my event scheduling application using the Django Rest Framework, but I'm having some problems using views that don't expect the HTTP GET method. I read the tutorial on the DRF website. From what I understand after reading the tutorial, and the class-based documentation on the Django site is that if there is such a class-based view (taken from the DRF tutorial)

class SnippetDetail(APIView): """ Retrieve, update or delete a snippet instance. """ def get_object(self, pk): try: return Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: raise Http404 def get(self, request, pk, format=None): snippet = self.get_object(pk) serializer = SnippetSerializer(snippet) return Response(serializer.data) def put(self, request, pk, format=None): snippet = self.get_object(pk) serializer = SnippetSerializer(snippet, data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): snippet = self.get_object(pk) snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT) 

Different methods in the view correspond to different HTTP request methods. Therefore, if I have www.foo.com/bar , this will do two different things based on which request method is sent to this address. Thus, this means that I will not need to specify anything else, because the function being executed is determined based on the method by which the URL is sent. Is it correct?

I have this view that I tried to model after an example on the DRF website

 class EventDetail(APIView): """ Retrieve, update or delete a event instance. """ def get_object(self, pk): try: return Event.objects.get(pk=pk) except Event.DoesNotExist: raise Http404 def get(self, request, pk, format=None): event = self.get_object(pk) serializer = EventSerializer(event) return Response(serializer.data) def post(self, request, format=None): serializer = EventSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) # def put(self, request, pk, format=None): # event = self.get_object(pk) # serializer = EventSerializer(event, data=request.DATA) # if serializer.is_valid(): # serializer.save() # return Response(serializer.data) # return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): event = self.get_object(pk) event.delete() return Response(status=status.HTTP_204_NO_CONTENT 

which displays these urls

 urlpatterns = patterns('', # Get event url(r'^(?P<pk>\d+)/$', views.EventDetail.as_view(), name='create_events'), # list all events url(r'^list/$', views.EventList.as_view(), name='list_events'), # url(r'^update$/(?P<pk>\d+)', #update event), url(r'^create/$', views.EventDetail.as_view(), name='create_events'), # delete event url(r'^delete$/(?P<pk>\d+)', views.EventDetail.as_view(), name='delete_event'), ) 

which I am trying to verify with CURL using this command (for example, here REMOVE using CURL with an encoded URL )

 curl -X DELETE "http://127.0.0.1:8000/events/delete/1" 

This seems to be doing what should:

 [18/Oct/2014 22:41:27] "DELETE /events/delete/1 HTTP/1.1" 404 2707 

But the actual record is not deleted from my database

Is there something here that I forget to do to make them work correctly?

+7
python rest django django-rest-framework
source share
2 answers

You are redundant. The HTTP method is already DELETE , so there is no /events/delete in the URL. Try the following:

 curl -X DELETE "http://127.0.0.1:8000/events/1/" 

By default, the DRF router creates detailed URLs in /event/<pk> , and you get GET , PUT , POST and DELETE to retrieve, update, create and delete them, respectively.

+16
source share

As mentioned by Kevin Stone, the template you use is not recommended, but if you want to use it, you need to correct the typo in your URLs for events / delete / match.

  # delete event url(r'^delete$/(?P<pk>\d+)', views.EventDetail.as_view(), name='delete_event'), 

it should be:

  # delete event url(r'^delete/(?P<pk>\d+)', views.EventDetail.as_view(), name='delete_event'), 
+2
source share

All Articles