Skip to content

Commit 08c727a

Browse files
committed
@api_view defaults to allowing GET
1 parent 3a5b377 commit 08c727a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

docs/api-guide/views.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,26 @@ REST framework also allows you to work with regular function based views. It pr
127127

128128
## @api_view()
129129

130-
**Signature:** `@api_view(http_method_names)`
130+
**Signature:** `@api_view(http_method_names=['GET'])`
131131

132-
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:
132+
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:
133133

134134
from rest_framework.decorators import api_view
135135

136-
@api_view(['GET'])
136+
@api_view()
137137
def hello_world(request):
138138
return Response({"message": "Hello, world!"})
139139

140-
141140
This view will use the default renderers, parsers, authentication classes etc specified in the [settings].
142141

142+
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:
143+
144+
@api_view(['GET', 'POST'])
145+
def hello_world(request):
146+
if request.method == 'POST':
147+
return Response({"message": "Got some data!", "data": request.data})
148+
return Response({"message": "Hello, world!"})
149+
143150
## API policy decorators
144151

145152
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:

rest_framework/decorators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import types
1313

1414

15-
def api_view(http_method_names):
15+
def api_view(http_method_names=None):
1616

1717
"""
1818
Decorator that converts a function-based view into an APIView subclass.
1919
Takes a list of allowed methods for the view as an argument.
2020
"""
21+
if http_method_names is None:
22+
http_method_names = ['GET']
2123

2224
def decorator(func):
2325

0 commit comments

Comments
 (0)