Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django view - APIView (retrieve, update or delete - GET, PUT, DELETE)

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)
Comment

django view - apiview decorator (retrieve, update or delete - GET, PUT, DELETE)

@api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request, pk):
    """
    Retrieve, update or 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 = SnippetSerializer(snippet)
        return Response(serializer.data)

    elif request.method == 'PUT':
        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)

    elif request.method == 'DELETE':
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Comment

PREVIOUS NEXT
Code Example
Python :: pandas cumulative mean 
Python :: entered_text_1 = textbox_1.get(1.0, tk.END+"-1c") 
Python :: escape character in python 
Python :: match python 3.10 
Python :: find the highest id in model django 
Python :: get name of month python 
Python :: hstack 
Python :: count_values in python 
Python :: Simple dictionary in Python 
Python :: Game of Piles Version 2 
Python :: selenium webdriver scroll down python 
Python :: write lines python with line breaks 
Python :: copy files to a directory using text file 
Python :: dataframe to ftp 
Python :: calculate days between two dates using python 
Python :: python read binary trj file 
Python :: how to add attribute to class python 
Python :: django only certain columns from database 
Python :: python filter dict 
Python :: match statement 
Python :: python list comprehension 
Python :: strftime python multiple formats 
Python :: add to python list 
Python :: python dunder 
Python :: print colored text in python 
Python :: factorial of a number in python 
Python :: domain name of my site 
Python :: python pandas table save 
Python :: Program for length of the shortest word 
Python :: fibonacci 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =