Permalink
Browse files

class-based view

  • Loading branch information...
junkboy0315 committed Feb 8, 2019
1 parent 0c9f284 commit 4585bb6229fddb875337ced280689abba1b2093b
Showing with 16 additions and 14 deletions.
  1. +2 −3 tutorial/snippets/urls.py
  2. +14 −11 tutorial/snippets/views.py
@@ -1,11 +1,10 @@
from django.urls import path from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.urlpatterns import format_suffix_patterns
from snippets import views from snippets.views import SnippetList, SnippetDetail
from snippets.views import SnippetList


urlpatterns = [ urlpatterns = [
path('snippets/', SnippetList.as_view()), path('snippets/', SnippetList.as_view()),
path('snippets/<int:pk>/', views.snippet_detail), path('snippets/<int:pk>/', SnippetDetail.as_view()),
] ]


urlpatterns = format_suffix_patterns(urlpatterns) urlpatterns = format_suffix_patterns(urlpatterns)
@@ -1,8 +1,7 @@
from django.http import Http404
from rest_framework import status from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView

from snippets.models import Snippet from snippets.models import Snippet
from snippets.serializers import SnippetSerializer from snippets.serializers import SnippetSerializer


@@ -27,28 +26,32 @@ def post(self, request, format=None):
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)




@api_view(['GET', 'PUT', 'DELETE']) class SnippetDetail(APIView):
def snippet_detail(request, pk, format=None):
""" """
単一のスニペットの、取得・更新・削除を行う。 単一のスニペットの、取得・更新・削除を行う。
""" """
try:
snippet = Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)


if request.method == 'GET': 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) serializer = SnippetSerializer(snippet)
return Response(serializer.data) return Response(serializer.data)


elif request.method == 'PUT': def put(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet, data=request.data) serializer = SnippetSerializer(snippet, data=request.data)
if not serializer.is_valid(): if not serializer.is_valid():
return Response(serializer.errors, return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
serializer.save() serializer.save()
return Response(serializer.data) return Response(serializer.data)


elif request.method == 'DELETE': def delete(self, request, pk, format=None):
snippet = self.get_object(pk)
snippet.delete() snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)

0 comments on commit 4585bb6

Please sign in to comment.