Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

DJANGO rest framework GET POST

from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer


@api_view(['GET', 'POST'])
def snippet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = SnippetSerializer(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)
Comment

PREVIOUS NEXT
Code Example
Python :: sns histplot 
Python :: numpy multiply element wise 
Python :: python sklearn knn regression example 
Python :: .replit file python 
Python :: Seaborn python for stacked column 
Python :: highlight null/nan values in pandas table 
Python :: create app in a django project 
Python :: basic flask app python 
Python :: for if else one line python 
Python :: python backslash in string 
Python :: console-based animation-simple 
Python :: save image to file from URL 
Python :: pack tkinter 
Python :: tqdm description 
Python :: Error: getaddrinfo ENOTFOUND www.python.org www.python.org:443 Downloading Python failed. Error: { Error: getaddrinfo ENOTFOUND www.python.org www.python.org:443 
Python :: find average with sum and len in python 
Python :: how to slice dataframe by timestamp 
Python :: for loop example python 3 
Python :: scikit tsne 
Python :: run python command 
Python :: python int string float 
Python :: How do I stop Selenium from closing my browser 
Python :: sympy 
Python :: int to hex python without 0x 
Python :: python remove by index 
Python :: load png to python 
Python :: how to handle missing values in dataset 
Python :: how to capture video in google colab with python 
Python :: continue python 
Python :: datetime.timedelta format to string python 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =