#Request Methods in Django REST Framework

7 messages · Page 1 of 1 (latest)

shy karma
#
@api_view(['DELETE']) # delete
def taskDelete(request, pk):
    task = Task.objects.get(id=pk)
    task.delete()
    return Response('Item successfully deleted!')

This is an example above.
Why can you have special request methods like DELETE, PUT, PATCH etc. in Django REST Framework, but not in plain Django? Is it possible to create an API using just Django, as opposed to DRF? (I am just starting out with learning Django REST Framework, to create APIs.)

fathom sundial
shy karma
#
@api_view(['POST']) # create
def taskCreate(request):
    serializer = TaskSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)

This is my API View for the Create method (adding a new task). Is there an alternative way to do this using Task.objects.create() method?

fathom sundial
#

yeah but dunno if I'd recommend it. You can accept post data in any format

#

so you could use a form rather than a serializer, but it doesn't understand JSON so you'll need to do some preparation, which is why serializers are so powerful

shy karma
#

CSRF Token in Django REST Framework

fathom sundial
#

you should really make a new post rather than re-using this one