Permalink
Browse files

class-based view

  • Loading branch information...
junkboy0315 committed Feb 8, 2019
1 parent 179077b commit 0c9f2846250543dd7ce69caa52e73240809a43fb
Showing with 7 additions and 5 deletions.
  1. +2 −1 tutorial/snippets/urls.py
  2. +5 −4 tutorial/snippets/views.py
@@ -1,9 +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 import views
from snippets.views import SnippetList


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


@@ -1,23 +1,24 @@
from rest_framework import status from rest_framework import status
from rest_framework.decorators import api_view 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 snippets.models import Snippet from snippets.models import Snippet
from snippets.serializers import SnippetSerializer from snippets.serializers import SnippetSerializer




@api_view(['GET', 'POST']) class SnippetList(APIView):
def snippet_list(request, format=None):
""" """
- 全ての snippets を表示する。 - 全ての snippets を表示する。
- 新しい snippet を作成する - 新しい snippet を作成する
""" """
if request.method == 'GET':
def get(self, request, format=None):
snippets = Snippet.objects.all() snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True) serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data) return Response(serializer.data)


elif request.method == 'POST': def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data) serializer = SnippetSerializer(data=request.data)
if not serializer.is_valid(): if not serializer.is_valid():
return Response(serializer.errors, return Response(serializer.errors,

0 comments on commit 0c9f284

Please sign in to comment.