aboutsummaryrefslogtreecommitdiffstats
path: root/docs/tutorial/3-class-based-views.md
diff options
context:
space:
mode:
authorTom Christie2012-09-03 16:42:57 +0100
committerTom Christie2012-09-03 16:42:57 +0100
commit1a1ccf94c2c0cadee8b0bd1c8f85c591c650d763 (patch)
tree216469f1a58a42f2078a2718f67b4ebcf7dd1128 /docs/tutorial/3-class-based-views.md
parent93189ec27d53d3216d452abdc2711e973a888d0c (diff)
downloaddjango-rest-framework-1a1ccf94c2c0cadee8b0bd1c8f85c591c650d763.tar.bz2
Fixes to APIView
Diffstat (limited to 'docs/tutorial/3-class-based-views.md')
-rw-r--r--docs/tutorial/3-class-based-views.md35
1 files changed, 12 insertions, 23 deletions
diff --git a/docs/tutorial/3-class-based-views.md b/docs/tutorial/3-class-based-views.md
index e56c7847..616a6058 100644
--- a/docs/tutorial/3-class-based-views.md
+++ b/docs/tutorial/3-class-based-views.md
@@ -11,9 +11,9 @@ We'll start by rewriting the root view as a class based view. All this involves
from django.http import Http404
from djangorestframework.views import APIView
from djangorestframework.response import Response
- from djangorestframework.status import *
+ from djangorestframework.status import status
- class CommentRoot(views.APIView):
+ class CommentRoot(APIView):
"""
List all comments, or create a new comment.
"""
@@ -28,20 +28,21 @@ We'll start by rewriting the root view as a class based view. All this involves
comment = serializer.object
comment.save()
return Response(serializer.serialized, status=HTTP_201_CREATED)
- else:
- return Response(serializer.serialized_errors, status=HTTP_400_BAD_REQUEST)
+ return Response(serializer.serialized_errors, status=HTTP_400_BAD_REQUEST)
+
+ comment_root = CommentRoot.as_view()
So far, so good. It looks pretty similar to the previous case, but we've got better seperation between the different HTTP methods. We'll also need to update the instance view.
- class CommentInstance(views.APIView):
+ class CommentInstance(APIView):
"""
Retrieve, update or delete a comment instance.
"""
def get_object(self, pk):
try:
- return Poll.objects.get(pk=pk)
- except Poll.DoesNotExist:
+ return Comment.objects.get(pk=pk)
+ except Comment.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
@@ -56,28 +57,16 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
comment = serializer.deserialized
comment.save()
return Response(serializer.data)
- else:
- return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
+ return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
comment = self.get_object(pk)
comment.delete()
- return Response(status=HTTP_204_NO_CONTENT)
-
-That's looking good. Again, it's still pretty similar to the function based view right now.
-
-Since we're now working with class based views, rather than function based views, we'll also need to update our urlconf slightly.
+ return Response(status=status.HTTP_204_NO_CONTENT)
- from blogpost import views
- from djangorestframework.urlpatterns import format_suffix_patterns
-
- urlpatterns = patterns('',
- url(r'^$', views.CommentRoot.as_view()),
- url(r'^(?P<id>[0-9]+)$', views.CommentInstance.as_view())
- )
-
- urlpatterns = format_suffix_patterns(urlpatterns)
+ comment_instance = CommentInstance.as_view()
+That's looking good. Again, it's still pretty similar to the function based view right now.
Okay, we're done. If you run the development server everything should be working just as before.
## Using mixins