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
:: compile python to exe bash 
Python :: python recursion factorial 
Python ::  
Python :: python sort multiple keys 
Python :: install pocketsphinx error 
Python :: django rest framework function based views 
Python ::  
Python :: python select from list by condition 
Python :: how to delete item from list python 
:: how to use def in python 
Python :: how to make tkinter look modern 
Python ::  
Python :: ValueError: cannot convert float NaN to integer 
Python :: python convert strings to chunks 
::  
Python :: hex to string python 
:: heroku[web.1]: Process exited with status 3 
Python :: break line in string python 
:: python tkinter messagebox 
Python :: pandas if python 
Python ::  
::  
Python :: Word2Vec 4.0 Gensim model python dataframe 
Python :: python np get indices where value 
:: return mean of df as dataframe 
Python :: numpy ndarray to matrix 
Python :: super in django manager 
Python :: if statement in python 
::  
Python ::  
ADD CONTENT
Topic
Content
Source link
Name
6+3 =