aboutsummaryrefslogtreecommitdiffstats
path: root/docs/tutorial/2-requests-and-responses.md
diff options
context:
space:
mode:
authorTom Christie2012-10-28 19:25:51 +0000
committerTom Christie2012-10-28 19:25:51 +0000
commitfde79376f323708d9f7b80ee830fe63060fb335f (patch)
treef8b2cf9b1bc9665fb2ebe2ffe3a61ba2ef0aca27 /docs/tutorial/2-requests-and-responses.md
parent12c363c1fe237d0357e6020b44890926856b9191 (diff)
downloaddjango-rest-framework-fde79376f323708d9f7b80ee830fe63060fb335f.tar.bz2
Pastebin tutorial
Diffstat (limited to 'docs/tutorial/2-requests-and-responses.md')
-rw-r--r--docs/tutorial/2-requests-and-responses.md45
1 files changed, 22 insertions, 23 deletions
diff --git a/docs/tutorial/2-requests-and-responses.md b/docs/tutorial/2-requests-and-responses.md
index fc37322a..76803d24 100644
--- a/docs/tutorial/2-requests-and-responses.md
+++ b/docs/tutorial/2-requests-and-responses.md
@@ -38,27 +38,27 @@ Okay, let's go ahead and start using these new components to write a few views.
We don't need our `JSONResponse` class anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.
- from blog.models import Comment
- from blog.serializers import CommentSerializer
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
+ from snippet.models import Snippet
+ from snippet.serializers import SnippetSerializer
+
@api_view(['GET', 'POST'])
- def comment_root(request):
+ def snippet_list(request):
"""
- List all comments, or create a new comment.
+ List all snippets, or create a new snippet.
"""
if request.method == 'GET':
- comments = Comment.objects.all()
- serializer = CommentSerializer(instance=comments)
+ snippets = Snippet.objects.all()
+ serializer = SnippetSerializer(instance=snippets)
return Response(serializer.data)
elif request.method == 'POST':
- serializer = CommentSerializer(request.DATA)
+ serializer = SnippetSerializer(request.DATA)
if serializer.is_valid():
- comment = serializer.object
- comment.save()
+ serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@@ -67,30 +67,29 @@ We don't need our `JSONResponse` class anymore, so go ahead and delete that. On
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.
@api_view(['GET', 'PUT', 'DELETE'])
- def comment_instance(request, pk):
+ def snippet_detail(request, pk):
"""
- Retrieve, update or delete a comment instance.
+ Retrieve, update or delete a snippet instance.
"""
try:
- comment = Comment.objects.get(pk=pk)
- except Comment.DoesNotExist:
+ snippet = Snippet.objects.get(pk=pk)
+ except Snippet.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
- serializer = CommentSerializer(instance=comment)
+ serializer = SnippetSerializer(instance=snippet)
return Response(serializer.data)
elif request.method == 'PUT':
- serializer = CommentSerializer(request.DATA, instance=comment)
+ serializer = SnippetSerializer(request.DATA, instance=snippet)
if serializer.is_valid():
- comment = serializer.object
- comment.save()
+ serializer.save()
return Response(serializer.data)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
- comment.delete()
+ snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
This should all feel very familiar - there's not a lot different to working with regular Django views.
@@ -103,20 +102,20 @@ To take advantage of the fact that our responses are no longer hardwired to a si
Start by adding a `format` keyword argument to both of the views, like so.
- def comment_root(request, format=None):
+ def snippet_list(request, format=None):
and
- def comment_instance(request, pk, format=None):
+ def snippet_detail(request, pk, format=None):
Now update the `urls.py` file slightly, to append a set of `format_suffix_patterns` in addition to the existing URLs.
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
- urlpatterns = patterns('blog.views',
- url(r'^$', 'comment_root'),
- url(r'^(?P<pk>[0-9]+)$', 'comment_instance')
+ urlpatterns = patterns('snippet.views',
+ url(r'^$', 'snippet_list'),
+ url(r'^(?P<pk>[0-9]+)$', 'snippet_detail')
)
urlpatterns = format_suffix_patterns(urlpatterns)