aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2012-09-03 16:42:57 +0100
committerTom Christie2012-09-03 16:42:57 +0100
commit1a1ccf94c2c0cadee8b0bd1c8f85c591c650d763 (patch)
tree216469f1a58a42f2078a2718f67b4ebcf7dd1128
parent93189ec27d53d3216d452abdc2711e973a888d0c (diff)
downloaddjango-rest-framework-1a1ccf94c2c0cadee8b0bd1c8f85c591c650d763.tar.bz2
Fixes to APIView
-rw-r--r--djangorestframework/views.py9
-rw-r--r--docs/tutorial/3-class-based-views.md35
2 files changed, 17 insertions, 27 deletions
diff --git a/djangorestframework/views.py b/djangorestframework/views.py
index 3b76988c..10b0dccb 100644
--- a/djangorestframework/views.py
+++ b/djangorestframework/views.py
@@ -12,10 +12,11 @@ from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.views.decorators.csrf import csrf_exempt
-from djangorestframework.compat import View as DjangoView, apply_markdown
+from djangorestframework.compat import View, apply_markdown
from djangorestframework.response import Response
from djangorestframework.request import Request
-from djangorestframework import renderers, parsers, authentication, permissions, status, exceptions
+from djangorestframework.settings import api_settings
+from djangorestframework import parsers, authentication, permissions, status, exceptions
__all__ = (
@@ -61,13 +62,13 @@ def _camelcase_to_spaces(content):
return re.sub(camelcase_boundry, ' \\1', content).strip()
-class View(DjangoView):
+class APIView(View):
"""
Handles incoming requests and maps them to REST operations.
Performs request deserialization, response serialization, authentication and input validation.
"""
- renderers = renderers.DEFAULT_RENDERERS
+ renderers = api_settings.DEFAULT_RENDERERS
"""
List of renderer classes the view can serialize the response with, ordered by preference.
"""
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