Created
March 10, 2014 17:40
-
-
Save lu911/9470051 to your computer and use it in GitHub Desktop.
Class based SnippetList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from snippets.models import Snippet | |
from snippets.serializers import SnippetSerializer | |
from django.http import Http404 | |
from rest_framework.views import APIView | |
from rest_framework.response import Response | |
from rest_framework import status | |
class SnippetList(APIView): | |
""" | |
List all snippets, or create a new snippet. | |
""" | |
def get(self, request, format=None): | |
snippets = Snippet.objects.all() | |
serializer = SnippetSerializer(snippets, many=True) | |
return Response(serializer.data) | |
def post(self, request, format=None): | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment