diff options
| author | Tom Christie | 2014-12-01 12:20:07 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-12-01 12:20:07 +0000 |
| commit | ed93e13a1c6f792e14176bdaa5e96d0fa2c63a2f (patch) | |
| tree | 03e176c54384ac88d22a1fbc4ba32a6e320695f2 /tutorial/2-requests-and-responses/index.html | |
| parent | 9defb5ee9f0090f98fa579f1e74b7dfdd6138744 (diff) | |
| download | django-rest-framework-ed93e13a1c6f792e14176bdaa5e96d0fa2c63a2f.tar.bz2 | |
Update documentation
Diffstat (limited to 'tutorial/2-requests-and-responses/index.html')
| -rw-r--r-- | tutorial/2-requests-and-responses/index.html | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/tutorial/2-requests-and-responses/index.html b/tutorial/2-requests-and-responses/index.html index e6dc3004..3ef5c326 100644 --- a/tutorial/2-requests-and-responses/index.html +++ b/tutorial/2-requests-and-responses/index.html @@ -165,6 +165,10 @@ </li> <li > + <a href="../../api-guide/validators">Validators</a> + </li> + + <li > <a href="../../api-guide/authentication">Authentication</a> </li> @@ -264,6 +268,10 @@ </li> <li > + <a href="../../topics/3.0-announcement">3.0 Announcement</a> + </li> + + <li > <a href="../../topics/kickstarter-announcement">Kickstarter Announcement</a> </li> @@ -390,9 +398,9 @@ <p>From this point we're going to really start covering the core of REST framework. Let's introduce a couple of essential building blocks.</p> <h2 id="request-objects">Request objects</h2> -<p>REST framework introduces a <code>Request</code> object that extends the regular <code>HttpRequest</code>, and provides more flexible request parsing. The core functionality of the <code>Request</code> object is the <code>request.DATA</code> attribute, which is similar to <code>request.POST</code>, but more useful for working with Web APIs.</p> +<p>REST framework introduces a <code>Request</code> object that extends the regular <code>HttpRequest</code>, and provides more flexible request parsing. The core functionality of the <code>Request</code> object is the <code>request.data</code> attribute, which is similar to <code>request.POST</code>, but more useful for working with Web APIs.</p> <pre><code>request.POST # Only handles form data. Only works for 'POST' method. -request.DATA # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods. +request.data # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods. </code></pre> <h2 id="response-objects">Response objects</h2> <p>REST framework also introduces a <code>Response</code> object, which is a type of <code>TemplateResponse</code> that takes unrendered content and uses content negotiation to determine the correct content type to return to the client.</p> @@ -407,7 +415,7 @@ request.DATA # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' met <li>The <code>APIView</code> class for working with class based views.</li> </ol> <p>These wrappers provide a few bits of functionality such as making sure you receive <code>Request</code> instances in your view, and adding context to <code>Response</code> objects so that content negotiation can be performed.</p> -<p>The wrappers also provide behaviour such as returning <code>405 Method Not Allowed</code> responses when appropriate, and handling any <code>ParseError</code> exception that occurs when accessing <code>request.DATA</code> with malformed input.</p> +<p>The wrappers also provide behaviour such as returning <code>405 Method Not Allowed</code> responses when appropriate, and handling any <code>ParseError</code> exception that occurs when accessing <code>request.data</code> with malformed input.</p> <h2 id="pulling-it-all-together">Pulling it all together</h2> <p>Okay, let's go ahead and start using these new components to write a few views.</p> <p>We don't need our <code>JSONResponse</code> class in <code>views.py</code> anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.</p> @@ -429,7 +437,7 @@ def snippet_list(request): return Response(serializer.data) elif request.method == 'POST': - serializer = SnippetSerializer(data=request.DATA) + serializer = SnippetSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) @@ -452,7 +460,7 @@ def snippet_detail(request, pk): return Response(serializer.data) elif request.method == 'PUT': - serializer = SnippetSerializer(snippet, data=request.DATA) + serializer = SnippetSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) @@ -463,7 +471,7 @@ def snippet_detail(request, pk): return Response(status=status.HTTP_204_NO_CONTENT) </code></pre> <p>This should all feel very familiar - it is not a lot different from working with regular Django views.</p> -<p>Notice that we're no longer explicitly tying our requests or responses to a given content type. <code>request.DATA</code> can handle incoming <code>json</code> requests, but it can also handle <code>yaml</code> and other formats. Similarly we're returning response objects with data, but allowing REST framework to render the response into the correct content type for us.</p> +<p>Notice that we're no longer explicitly tying our requests or responses to a given content type. <code>request.data</code> can handle incoming <code>json</code> requests, but it can also handle <code>yaml</code> and other formats. Similarly we're returning response objects with data, but allowing REST framework to render the response into the correct content type for us.</p> <h2 id="adding-optional-format-suffixes-to-our-urls">Adding optional format suffixes to our URLs</h2> <p>To take advantage of the fact that our responses are no longer hardwired to a single content type let's add support for format suffixes to our API endpoints. Using format suffixes gives us URLs that explicitly refer to a given format, and means our API will be able to handle URLs such as <a href="http://example.com/api/items/4.json">http://example.com/api/items/4.json</a>.</p> <p>Start by adding a <code>format</code> keyword argument to both of the views, like so.</p> @@ -532,7 +540,7 @@ curl -X POST http://127.0.0.1:8000/snippets/ -d '{"code": "print 456"}' -H "Cont <!--/.wrapper --> <footer class="span12"> - <p>Sponsored by <a href="http://dabapps.com/">DabApps</a>.</a> + <p>Documentation built with <a href="http://www.mkdocs.org/">MkDocs</a>.</a> </p> </footer> |
