diff options
Diffstat (limited to 'api-guide/views/index.html')
| -rw-r--r-- | api-guide/views/index.html | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/api-guide/views/index.html b/api-guide/views/index.html index f6c931a4..eac4c5c8 100644 --- a/api-guide/views/index.html +++ b/api-guide/views/index.html @@ -165,6 +165,10 @@ </li> <li > + <a href="../validators">Validators</a> + </li> + + <li > <a href="../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> @@ -478,15 +486,22 @@ This method is used to enforce permissions and throttling, and perform content n </blockquote> <p>REST framework also allows you to work with regular function based views. It provides a set of simple decorators that wrap your function based views to ensure they receive an instance of <code>Request</code> (rather than the usual Django <code>HttpRequest</code>) and allows them to return a <code>Response</code> (instead of a Django <code>HttpResponse</code>), and allow you to configure how the request is processed.</p> <h2 id="api_view">@api_view()</h2> -<p><strong>Signature:</strong> <code>@api_view(http_method_names)</code></p> -<p>The core of this functionality is the <code>api_view</code> decorator, which takes a list of HTTP methods that your view should respond to. For example, this is how you would write a very simple view that just manually returns some data:</p> +<p><strong>Signature:</strong> <code>@api_view(http_method_names=['GET'])</code></p> +<p>The core of this functionality is the <code>api_view</code> decorator, which takes a list of HTTP methods that your view should respond to. For example, this is how you would write a very simple view that just manually returns some data:</p> <pre><code>from rest_framework.decorators import api_view -@api_view(['GET']) +@api_view() def hello_world(request): return Response({"message": "Hello, world!"}) </code></pre> <p>This view will use the default renderers, parsers, authentication classes etc specified in the <a href="../settings">settings</a>.</p> +<p>By default only <code>GET</code> methods will be accepted. Other methods will respond with "405 Method Not Allowed". To alter this behavior, specify which methods the view allows, like so:</p> +<pre><code>@api_view(['GET', 'POST']) +def hello_world(request): + if request.method == 'POST': + return Response({"message": "Got some data!", "data": request.data}) + return Response({"message": "Hello, world!"}) +</code></pre> <h2 id="api-policy-decorators">API policy decorators</h2> <p>To override the default settings, REST framework provides a set of additional decorators which can be added to your views. These must come <em>after</em> (below) the <code>@api_view</code> decorator. For example, to create a view that uses a <a href="../throttling">throttle</a> to ensure it can only be called once per day by a particular user, use the <code>@throttle_classes</code> decorator, passing a list of throttle classes:</p> <pre><code>from rest_framework.decorators import api_view, throttle_classes @@ -524,7 +539,7 @@ def view(request): <!--/.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> |
