diff --git a/docs/tutorial/2-requests-and-responses.md b/docs/tutorial/2-requests-and-responses.md index e2c173d6e..1bc6f768e 100644 --- a/docs/tutorial/2-requests-and-responses.md +++ b/docs/tutorial/2-requests-and-responses.md @@ -47,7 +47,7 @@ We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and de @api_view(['GET', 'POST']) def snippet_list(request): """ - List all snippets, or create a new snippet. + List all snippets or create a new snippet. """ if request.method == 'GET': snippets = Snippet.objects.all() @@ -59,7 +59,8 @@ We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and de if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious. @@ -68,7 +69,7 @@ Here is the view for an individual snippet, in the `views.py` module. @api_view(['GET', 'PUT', 'DELETE']) def snippet_detail(request, pk): """ - Retrieve, update or delete a snippet instance. + Retrieve, update, or delete a snippet instance. """ try: snippet = Snippet.objects.get(pk=pk) @@ -84,7 +85,8 @@ Here is the view for an individual snippet, in the `views.py` module. if serializer.is_valid(): serializer.save() return Response(serializer.data) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() diff --git a/docs/tutorial/3-class-based-views.md b/docs/tutorial/3-class-based-views.md index abf82e495..6fb7d7f0c 100644 --- a/docs/tutorial/3-class-based-views.md +++ b/docs/tutorial/3-class-based-views.md @@ -16,8 +16,10 @@ We'll start by rewriting the root view as a class based view. All this involves class SnippetList(APIView): """ - List all snippets, or create a new snippet. + List all snippets or create a new snippet. """ + queryset = Snippet.objects.all() + def get(self, request, format=None): snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) @@ -28,14 +30,17 @@ We'll start by rewriting the root view as a class based view. All this involves if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`. class SnippetDetail(APIView): """ - Retrieve, update or delete a snippet instance. + Retrieve, update, or delete a snippet instance. """ + queryset = Snippet.objects.all() + def get_object(self, pk): try: return Snippet.objects.get(pk=pk) @@ -53,7 +58,8 @@ So far, so good. It looks pretty similar to the previous case, but we've got be if serializer.is_valid(): serializer.save() return Response(serializer.data) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk, format=None): snippet = self.get_object(pk)