Skip to content

Commit

Permalink
Added API views based views
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Mar 13, 2018
1 parent 80a5351 commit a4b94c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/views-and-generic-views.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Views and Generic Views
============================

To start with, we will use the :code:`APIView` to build the polls list and poll detail API we built in the chapter, doc:apis-without-drf:.




Creating Views
----------------
Expand Down
22 changes: 22 additions & 0 deletions pollsapi/polls/apiviews.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.shortcuts import render, get_object_or_404

from .serializers import PollSerializer
from .models import Poll

from rest_framework.views import APIView
from rest_framework.response import Response

class PollList(APIView):
def get(self, request):
polls = Poll.objects.all()[:20]
data = PollSerializer(polls, many=True).data
return Response(data)



class PollDetail(APIView):
def get(self, request, pk):
poll = get_object_or_404(Poll, pk=pk)
data = PollSerializer(poll).data
return Response(data)

File renamed without changes.
6 changes: 3 additions & 3 deletions pollsapi/polls/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.urls import path

from .views import polls_list, polls_detail

from .apiviews import PollList, PollDetail
urlpatterns = [
path("polls/", polls_list, name="polls_list"),
path("polls/<int:pk>/", polls_detail, name="polls_detail")
path("polls/", PollList.as_view(), name="polls_list"),
path("polls/<int:pk>/", PollDetail.as_view(), name="polls_detail")
]

0 comments on commit a4b94c0

Please sign in to comment.