diff options
| author | BrickXu | 2014-12-02 12:52:36 +0800 |
|---|---|---|
| committer | BrickXu | 2014-12-02 12:52:36 +0800 |
| commit | 81870b6e1a7b0c3c149d4bfba0e20924ebf1b187 (patch) | |
| tree | 4acb295a3c941db00b36f54f906d4e4e24e5bcf4 /docs/api-guide/views.md | |
| parent | 71654967325ff5c587ce37031109152e741786ee (diff) | |
| parent | d1fe61ce94af1e942f8d1026fb84b1909c230779 (diff) | |
| download | django-rest-framework-81870b6e1a7b0c3c149d4bfba0e20924ebf1b187.tar.bz2 | |
Merge pull request #2 from tomchristie/master
Merge upstream
Diffstat (limited to 'docs/api-guide/views.md')
| -rw-r--r-- | docs/api-guide/views.md | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/docs/api-guide/views.md b/docs/api-guide/views.md index 194a7a6b..291fe737 100644 --- a/docs/api-guide/views.md +++ b/docs/api-guide/views.md @@ -1,4 +1,5 @@ -<a class="github" href="decorators.py"></a> <a class="github" href="views.py"></a> +source: decorators.py + views.py # Class Based Views @@ -26,7 +27,7 @@ For example: class ListUsers(APIView): """ View to list all users in the system. - + * Requires token authentication. * Only admin users are able to access this view. """ @@ -54,7 +55,7 @@ The following attributes control the pluggable aspects of API views. ### .permission_classes -### .content_negotiation_class +### .content_negotiation_class ## API policy instantiation methods @@ -126,19 +127,26 @@ REST framework also allows you to work with regular function based views. It pr ## @api_view() -**Signature:** `@api_view(http_method_names)` +**Signature:** `@api_view(http_method_names=['GET'])` -The core of this functionality is the `api_view` 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: +The core of this functionality is the `api_view` 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: from rest_framework.decorators import api_view - @api_view(['GET']) + @api_view() def hello_world(request): return Response({"message": "Hello, world!"}) - This view will use the default renderers, parsers, authentication classes etc specified in the [settings]. +By default only `GET` 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: + + @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!"}) + ## API policy decorators To override the default settings, REST framework provides a set of additional decorators which can be added to your views. These must come *after* (below) the `@api_view` decorator. For example, to create a view that uses a [throttle][throttling] to ensure it can only be called once per day by a particular user, use the `@throttle_classes` decorator, passing a list of throttle classes: |
