mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-09 08:00:52 +03:00
@api_view defaults to allowing GET
This commit is contained in:
parent
3a5b3772fe
commit
08c727add3
|
@ -127,19 +127,26 @@ REST framework also allows you to work with regular function based views. It pr
|
||||||
|
|
||||||
## @api_view()
|
## @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
|
from rest_framework.decorators import api_view
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view()
|
||||||
def hello_world(request):
|
def hello_world(request):
|
||||||
return Response({"message": "Hello, world!"})
|
return Response({"message": "Hello, world!"})
|
||||||
|
|
||||||
|
|
||||||
This view will use the default renderers, parsers, authentication classes etc specified in the [settings].
|
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
|
## 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:
|
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:
|
||||||
|
|
|
@ -12,12 +12,14 @@ from rest_framework.views import APIView
|
||||||
import types
|
import types
|
||||||
|
|
||||||
|
|
||||||
def api_view(http_method_names):
|
def api_view(http_method_names=None):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Decorator that converts a function-based view into an APIView subclass.
|
Decorator that converts a function-based view into an APIView subclass.
|
||||||
Takes a list of allowed methods for the view as an argument.
|
Takes a list of allowed methods for the view as an argument.
|
||||||
"""
|
"""
|
||||||
|
if http_method_names is None:
|
||||||
|
http_method_names = ['GET']
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user