diff options
| author | Tom Christie | 2012-09-17 20:19:45 +0100 | 
|---|---|---|
| committer | Tom Christie | 2012-09-17 20:19:45 +0100 | 
| commit | 308677037f1b1f2edbd2527beac8505033c98bdc (patch) | |
| tree | ca71c4429058c769dd17283a4da75d9a01409fce /docs/tutorial/3-class-based-views.md | |
| parent | 549ebdc1c67c20bdff39a2f4a59012dd010213a1 (diff) | |
| download | django-rest-framework-308677037f1b1f2edbd2527beac8505033c98bdc.tar.bz2 | |
Tweak docs, fix .error_data -> .errors
Diffstat (limited to 'docs/tutorial/3-class-based-views.md')
| -rw-r--r-- | docs/tutorial/3-class-based-views.md | 29 | 
1 files changed, 15 insertions, 14 deletions
| diff --git a/docs/tutorial/3-class-based-views.md b/docs/tutorial/3-class-based-views.md index 24785179..3c8f1207 100644 --- a/docs/tutorial/3-class-based-views.md +++ b/docs/tutorial/3-class-based-views.md @@ -31,8 +31,6 @@ We'll start by rewriting the root view as a class based view.  All this involves                  return Response(serializer.serialized, status=status.HTTP_201_CREATED)              return Response(serializer.serialized_errors, status=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(APIView): @@ -58,16 +56,28 @@ 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) -            return Response(serializer.error_data, status=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=status.HTTP_204_NO_CONTENT) -    comment_instance = CommentInstance.as_view() -  That's looking good.  Again, it's still pretty similar to the function based view right now. + +We'll also need to refactor our URLconf slightly now we're using class based views. + +    from django.conf.urls import patterns, url +    from djangorestframework.urlpatterns import format_suffix_patterns +    from blogpost import views + +    urlpatterns = patterns('', +        url(r'^$', views.CommentRoot.as_view()), +        url(r'^(?P<pk>[0-9]+)$', views.CommentInstance.as_view()) +    ) +     +    urlpatterns = format_suffix_patterns(urlpatterns) +  Okay, we're done.  If you run the development server everything should be working just as before.  ## Using mixins @@ -95,8 +105,6 @@ Let's take a look at how we can compose our views by using the mixin classes.          def post(self, request, *args, **kwargs):              return self.create(request, *args, **kwargs) -    comment_root = CommentRoot.as_view() -  We'll take a moment to examine exactly what's happening here - We're building our view using `MultipleObjectBaseView`, and adding in `ListModelMixin` and `CreateModelMixin`.  The base class provides the core functionality, and the mixin classes provide the `.list()` and `.create()` actions.  We're then explictly binding the `get` and `post` methods to the appropriate actions.  Simple enough stuff so far. @@ -117,8 +125,6 @@ The base class provides the core functionality, and the mixin classes provide th          def delete(self, request, *args, **kwargs):              return self.destroy(request, *args, **kwargs) -    comment_instance = CommentInstance.as_view() -  Pretty similar.  This time we're using the `SingleObjectBaseView` class to provide the core functionality, and adding in mixins to provide the `.retrieve()`, `.update()` and `.destroy()` actions.  ## Using generic class based views @@ -134,16 +140,11 @@ Using the mixin classes we've rewritten the views to use slightly less code than          model = Comment          serializer_class = CommentSerializer -    comment_root = CommentRoot.as_view() -      class CommentInstance(generics.InstanceAPIView):          model = Comment          serializer_class = CommentSerializer -    comment_instance = CommentInstance.as_view() - -  Wow, that's pretty concise.  We've got a huge amount for free, and our code looks like good, clean, idomatic Django.  Next we'll move onto [part 4 of the tutorial][2], where we'll take a look at how we can  customize the behavior of our views to support a range of authentication, permissions, throttling and other aspects. | 
