From b907a4448572e3c48137e983248fb7411246563a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 18 Nov 2013 15:50:29 +0000 Subject: Remove .html suffixes --- tutorial/1-serialization.html | 89 ++++++++++---------- tutorial/2-requests-and-responses.html | 91 ++++++++++----------- tutorial/3-class-based-views.html | 87 ++++++++++---------- tutorial/4-authentication-and-permissions.html | 94 +++++++++++----------- tutorial/5-relationships-and-hyperlinked-apis.html | 87 ++++++++++---------- tutorial/6-viewsets-and-routers.html | 85 +++++++++---------- tutorial/quickstart.html | 87 ++++++++++---------- 7 files changed, 315 insertions(+), 305 deletions(-) (limited to 'tutorial') diff --git a/tutorial/1-serialization.html b/tutorial/1-serialization.html index 47bb9504..8028f27a 100644 --- a/tutorial/1-serialization.html +++ b/tutorial/1-serialization.html @@ -4,6 +4,7 @@
This tutorial will cover creating a simple pastebin code highlighting Web API. Along the way it will introduce the various components that make up REST framework, and give you a comprehensive understanding of how everything fits together.
-The tutorial is fairly in-depth, so you should probably get a cookie and a cup of your favorite brew before getting started. If you just want a quick overview, you should head over to the quickstart documentation instead.
+The tutorial is fairly in-depth, so you should probably get a cookie and a cup of your favorite brew before getting started. If you just want a quick overview, you should head over to the quickstart documentation instead.
Note: The code for this tutorial is available in the tomchristie/rest-framework-tutorial repository on GitHub. The completed implementation is also online as a sandbox version for testing, available here.
We're doing okay so far, we've got a serialization API that feels pretty similar to Django's Forms API, and some regular Django views.
Our API views don't do anything particularly special at the moment, beyond serving json responses, and there are some error handling edge cases we'd still like to clean up, but it's a functioning Web API.
We'll see how we can start to improve things in part 2 of the tutorial.
+We'll see how we can start to improve things in part 2 of the tutorial.
We don't necessarily need to add these extra url patterns in, but it gives us a simple, clean way of referring to a specific format.
Go ahead and test the API from the command line, as we did in tutorial part 1. Everything is working pretty similarly, although we've got some nicer error handling if we send invalid requests.
+Go ahead and test the API from the command line, as we did in tutorial part 1. Everything is working pretty similarly, although we've got some nicer error handling if we send invalid requests.
We can get a list of all of the snippets, as before.
curl http://127.0.0.1:8000/snippets/
@@ -336,9 +337,9 @@ curl -X POST http://127.0.0.1:8000/snippets/ -d '{"code": "print 456"}' -H "Cont
Browsability
Because the API chooses the content type of the response based on the client request, it will, by default, return an HTML-formatted representation of the resource when that resource is requested by a web browser. This allows for the API to return a fully web-browsable HTML representation.
Having a web-browsable API is a huge usability win, and makes developing and using your API much easier. It also dramatically lowers the barrier-to-entry for other developers wanting to inspect and work with your API.
-See the browsable api topic for more information about the browsable API feature and how to customize it.
+See the browsable api topic for more information about the browsable API feature and how to customize it.
What's next?
-In tutorial part 3, we'll start using class based views, and see how generic views reduce the amount of code we need to write.
+In tutorial part 3, we'll start using class based views, and see how generic views reduce the amount of code we need to write.
Wow, that's pretty concise. We've gotten a huge amount for free, and our code looks like good, clean, idiomatic Django.
-Next we'll move onto part 4 of the tutorial, where we'll take a look at how we can deal with authentication and permissions for our API.
+Next we'll move onto part 4 of the tutorial, where we'll take a look at how we can deal with authentication and permissions for our API.
Because 'snippets' is a reverse relationship on the User model, it will not be included by default when using the ModelSerializer class, so we needed to add an explicit field for it.
We'll also add a couple of views to views.py. We'd like to just use read-only views for the user representations, so we'll use the ListAPIView and RetrieveAPIView generic class based views.
class UserList(generics.ListAPIView):
+from django.contrib.auth.models import User
+
+
+class UserList(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
@@ -347,7 +351,7 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
Now, if you open a browser again, you find that the 'DELETE' and 'PUT' actions only appear on a snippet instance endpoint if you're logged in as the same user that created the code snippet.
Authenticating with the API
-Because we now have a set of permissions on the API, we need to authenticate our requests to it if we want to edit any snippets. We haven't set up any authentication classes, so the defaults are currently applied, which are SessionAuthentication and BasicAuthentication.
+Because we now have a set of permissions on the API, we need to authenticate our requests to it if we want to edit any snippets. We haven't set up any authentication classes, so the defaults are currently applied, which are SessionAuthentication and BasicAuthentication.
When we interact with the API through the web browser, we can login, and the browser session will then provide the required authentication for the requests.
If we're interacting with the API programmatically we need to explicitly provide the authentication credentials on each request.
If we try to create a snippet without authenticating, we'll get an error:
@@ -362,7 +366,7 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
We've now got a fairly fine-grained set of permissions on our Web API, and end points for users of the system and for the code snippets that they have created.
-In part 5 of the tutorial we'll look at how we can tie everything together by creating an HTML endpoint for our highlighted snippets, and improve the cohesion of our API by using hyperlinking for the relationships within the system.
+In part 5 of the tutorial we'll look at how we can tie everything together by creating an HTML endpoint for our highlighted snippets, and improve the cohesion of our API by using hyperlinking for the relationships within the system.
If we open a browser and navigate to the browsable API, you'll find that you can now work your way around the API simply by following links.
You'll also be able to see the 'highlight' links on the snippet instances, that will take you to the highlighted code HTML representations.
-In part 6 of the tutorial we'll look at how we can use ViewSets and Routers to reduce the amount of code we need to build our API.
+In part 6 of the tutorial we'll look at how we can use ViewSets and Routers to reduce the amount of code we need to build our API.
Or directly through the browser...

Great, that was easy!
-If you want to get a more in depth understanding of how REST framework fits together head on over to the tutorial, or start browsing the API guide.
+If you want to get a more in depth understanding of how REST framework fits together head on over to the tutorial, or start browsing the API guide.