def post(self, request, format=None): serializer = SnippetSerializers(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): snippet = self.get_object(pk=pk) serializer = SnippetSerializers(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 snippet_detail_django_format(request, pk): """ Retrieve, Update, Delete a Code Snippet. """ try: snippet = Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = SnippetSerializers(snippet) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = SnippetSerializers(snippet, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() return HttpResponse(status=204)
def snippet_list_function_based(request, format=None): """ Basic DRF Format List all the code Snippets or Create a new Snippet. """ if request.method == 'GET': snippets = Snippet.objects.all() serializer = SnippetSerializers(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = SnippetSerializers(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 snippet_list_django_format(request): """ Basic Django Format List all the code Snippets or Create a new Snippet. """ if request.method == 'GET': snippets = Snippet.objects.all() serializer = SnippetSerializers(snippets, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = SnippetSerializers(data=data) if serializer.is_valid(): serializer.save() JsonResponse(serializer.data, status=201) else: JsonResponse(serializer.errors, status=400)
def snippet_detail_function_based(request, pk, format=None): """ Retrieve, Update, Delete a Code Snippet. """ try: snippet = Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = SnippetSerializers(snippet) return Response(serializer.data) elif request.method == 'PUT': serializer = SnippetSerializers(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, pk, format=None): snippet = self.get_object(pk=pk) serializer = SnippetSerializers(snippet) return Response(serializer.data)
def get(self, request, format=None): snippets = Snippet.objects.all() serializer = SnippetSerializers(snippets, many=True) return Response(serializer.data)