22from .serializers import FooSerializer , BarSerializer
33
44from rest_framework import viewsets
5+ from rest_framework .decorators import api_view
6+ from rest_framework .views import APIView
7+ from rest_framework .request import Request
8+ from rest_framework .response import Response
59
6-
10+ # Viewsets
11+ # see https://www.django-rest-framework.org/tutorial/quickstart/
712class FooViewSet (viewsets .ModelViewSet ):
813 queryset = Foo .objects .all ()
914 serializer_class = FooSerializer
@@ -13,8 +18,25 @@ class BarViewSet(viewsets.ModelViewSet):
1318 queryset = Bar .objects .all ()
1419 serializer_class = BarSerializer
1520
21+ # class based view
22+ # see https://www.django-rest-framework.org/api-guide/views/#class-based-views
23+
24+ class MyClass (APIView ):
25+ def initial (self , request , * args , ** kwargs ):
26+ # this method will be called before processing any request
27+ super ().initial (request , * args , ** kwargs )
28+
29+ def get (self , request ):
30+ return Response ("GET request" )
31+
32+ def post (self , request ):
33+ return Response ("POST request" )
34+
35+
36+ # function based view
37+ # see https://www.django-rest-framework.org/api-guide/views/#function-based-views
38+
1639
17- # this is pure django
18- from django .http import HttpResponse , HttpRequest
19- def example (request : HttpRequest ):
20- return HttpResponse ("example" )
40+ @api_view (["GET" , "POST" ])
41+ def function_based_view (request : Request ):
42+ return Response ({"message" : "Hello, world!" })
0 commit comments