From 66cc2d3d5d611c57638a59932cd65c62d9b130c2 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 23 Dec 2013 12:37:56 +0000 Subject: Switch title ordering. --- tutorial/1-serialization.html | 8 +++----- tutorial/2-requests-and-responses.html | 8 +++----- tutorial/3-class-based-views.html | 2 +- tutorial/4-authentication-and-permissions.html | 6 +++--- tutorial/5-relationships-and-hyperlinked-apis.html | 2 +- tutorial/6-viewsets-and-routers.html | 2 +- tutorial/quickstart.html | 2 +- 7 files changed, 13 insertions(+), 17 deletions(-) (limited to 'tutorial') diff --git a/tutorial/1-serialization.html b/tutorial/1-serialization.html index c75116d3..76c7f818 100644 --- a/tutorial/1-serialization.html +++ b/tutorial/1-serialization.html @@ -2,7 +2,7 @@ - Django REST framework - Tutorial 1: Serialization + Tutorial 1: Serialization - Django REST framework @@ -431,8 +431,7 @@ def snippet_list(request): if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) - else: - return JSONResponse(serializer.errors, status=400) + return JSONResponse(serializer.errors, status=400)

Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as csrf_exempt. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.

We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.

@@ -456,8 +455,7 @@ def snippet_detail(request, pk): if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) - else: - return JSONResponse(serializer.errors, status=400) + return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() diff --git a/tutorial/2-requests-and-responses.html b/tutorial/2-requests-and-responses.html index 6b5f67da..ef821cec 100644 --- a/tutorial/2-requests-and-responses.html +++ b/tutorial/2-requests-and-responses.html @@ -2,7 +2,7 @@ - Django REST framework - Tutorial 2: Requests and Responses + Tutorial 2: Requests and Responses - Django REST framework @@ -255,8 +255,7 @@ def snippet_list(request): if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - else: - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + 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.

Here is the view for an individual snippet, in the views.py module.

@@ -279,8 +278,7 @@ def snippet_detail(request, pk): if serializer.is_valid(): serializer.save() return Response(serializer.data) - else: - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() diff --git a/tutorial/3-class-based-views.html b/tutorial/3-class-based-views.html index 6a5ca721..c807ad0f 100644 --- a/tutorial/3-class-based-views.html +++ b/tutorial/3-class-based-views.html @@ -2,7 +2,7 @@ - Django REST framework - Tutorial 3: Class Based Views + Tutorial 3: Class Based Views - Django REST framework diff --git a/tutorial/4-authentication-and-permissions.html b/tutorial/4-authentication-and-permissions.html index 6e166c08..dc1e2212 100644 --- a/tutorial/4-authentication-and-permissions.html +++ b/tutorial/4-authentication-and-permissions.html @@ -2,7 +2,7 @@ - Django REST framework - Tutorial 4: Authentication & Permissions + Tutorial 4: Authentication & Permissions - Django REST framework @@ -337,10 +337,10 @@ class IsOwnerOrReadOnly(permissions.BasePermission): def has_object_permission(self, request, view, obj): # Read permissions are allowed to any request, # so we'll always allow GET, HEAD or OPTIONS requests. - if request.method in permissions.SAFE_METHODS: + if request.method in permissions.SAFE_METHODS: return True - # Write permissions are only allowed to the owner of the snippet + # Write permissions are only allowed to the owner of the snippet. return obj.owner == request.user

Now we can add that custom permission to our snippet instance endpoint, by editing the permission_classes property on the SnippetDetail class:

diff --git a/tutorial/5-relationships-and-hyperlinked-apis.html b/tutorial/5-relationships-and-hyperlinked-apis.html index 9222bc2d..9b0b5c82 100644 --- a/tutorial/5-relationships-and-hyperlinked-apis.html +++ b/tutorial/5-relationships-and-hyperlinked-apis.html @@ -2,7 +2,7 @@ - Django REST framework - Tutorial 5: Relationships & Hyperlinked APIs + Tutorial 5: Relationships & Hyperlinked APIs - Django REST framework diff --git a/tutorial/6-viewsets-and-routers.html b/tutorial/6-viewsets-and-routers.html index a5287a79..d178e26d 100644 --- a/tutorial/6-viewsets-and-routers.html +++ b/tutorial/6-viewsets-and-routers.html @@ -2,7 +2,7 @@ - Django REST framework - Tutorial 6: ViewSets & Routers + Tutorial 6: ViewSets & Routers - Django REST framework diff --git a/tutorial/quickstart.html b/tutorial/quickstart.html index 842cacb1..fda9ffdc 100644 --- a/tutorial/quickstart.html +++ b/tutorial/quickstart.html @@ -2,7 +2,7 @@ - Django REST framework - Quickstart + Quickstart - Django REST framework -- cgit v1.2.3