You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -7,14 +7,18 @@ Let's introduce a couple of essential building blocks.
7
7
8
8
REST framework introduces a `Request` object that extends the regular `HttpRequest`, and provides more flexible request parsing. The core functionality of the `Request` object is the `request.data` attribute, which is similar to `request.POST`, but more useful for working with Web APIs.
9
9
10
-
request.POST # Only handles form data. Only works for 'POST' method.
11
-
request.data # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods.
10
+
```python
11
+
request.POST# Only handles form data. Only works for 'POST' method.
12
+
request.data # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods.
13
+
```
12
14
13
15
## Response objects
14
16
15
17
REST framework also introduces a `Response` object, which is a type of `TemplateResponse` that takes unrendered content and uses content negotiation to determine the correct content type to return to the client.
16
18
17
-
return Response(data) # Renders to content type as requested by the client.
19
+
```python
20
+
return Response(data) # Renders to content type as requested by the client.
21
+
```
18
22
19
23
## Status codes
20
24
@@ -35,87 +39,90 @@ The wrappers also provide behavior such as returning `405 Method Not Allowed` re
35
39
36
40
Okay, let's go ahead and start using these new components to refactor our views slightly.
37
41
38
-
from rest_framework import status
39
-
from rest_framework.decorators import api_view
40
-
from rest_framework.response import Response
41
-
from snippets.models import Snippet
42
-
from snippets.serializers import SnippetSerializer
Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious.
63
69
64
70
Here is the view for an individual snippet, in the `views.py` module.
This should all feel very familiar - it is not a lot different from working with regular Django views.
92
100
93
101
Notice that we're no longer explicitly tying our requests or responses to a given content type. `request.data` can handle incoming `json` requests, but it can also handle 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.
94
102
95
103
## Adding optional format suffixes to our URLs
96
104
97
-
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 [http://example.com/api/items/4.json][json-url].
105
+
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 [<http://example.com/api/items/4.json>][json-url].
98
106
99
107
Start by adding a `format` keyword argument to both of the views, like so.
100
-
101
-
def snippet_list(request, format=None):
102
-
108
+
`def snippet_list(request, format=None):`
103
109
and
104
-
105
-
def snippet_detail(request, pk, format=None):
110
+
`def snippet_detail(request, pk, format=None):`
106
111
107
112
Now update the `snippets/urls.py` file slightly, to append a set of `format_suffix_patterns` in addition to the existing URLs.
108
113
109
-
from django.urls import path
110
-
from rest_framework.urlpatterns import format_suffix_patterns
111
-
from snippets import views
114
+
```python
115
+
from django.urls import path
116
+
from rest_framework.urlpatterns import format_suffix_patterns
117
+
from snippets import views
112
118
113
-
urlpatterns = [
114
-
path('snippets/', views.snippet_list),
115
-
path('snippets/<int:pk>/', views.snippet_detail),
116
-
]
119
+
urlpatterns = [
120
+
path("snippets/", views.snippet_list),
121
+
path("snippets/<int:pk>/", views.snippet_detail),
122
+
]
117
123
118
-
urlpatterns = format_suffix_patterns(urlpatterns)
124
+
urlpatterns = format_suffix_patterns(urlpatterns)
125
+
```
119
126
120
127
We don't necessarily need to add these extra url patterns in, but it gives us a simple, clean way of referring to a specific format.
121
128
@@ -125,68 +132,76 @@ Go ahead and test the API from the command line, as we did in [tutorial part 1][
125
132
126
133
We can get a list of all of the snippets, as before.
127
134
128
-
http http://127.0.0.1:8000/snippets/
129
-
130
-
HTTP/1.1 200 OK
131
-
...
132
-
[
133
-
{
134
-
"id": 1,
135
-
"title": "",
136
-
"code": "foo = \"bar\"\n",
137
-
"linenos": false,
138
-
"language": "python",
139
-
"style": "friendly"
140
-
},
141
-
{
142
-
"id": 2,
143
-
"title": "",
144
-
"code": "print(\"hello, world\")\n",
145
-
"linenos": false,
146
-
"language": "python",
147
-
"style": "friendly"
148
-
}
149
-
]
135
+
```bash
136
+
http http://127.0.0.1:8000/snippets/
137
+
138
+
HTTP/1.1 200 OK
139
+
...
140
+
[
141
+
{
142
+
"id": 1,
143
+
"title": "",
144
+
"code": "foo = \"bar\"\n",
145
+
"linenos": false,
146
+
"language": "python",
147
+
"style": "friendly"
148
+
},
149
+
{
150
+
"id": 2,
151
+
"title": "",
152
+
"code": "print(\"hello, world\")\n",
153
+
"linenos": false,
154
+
"language": "python",
155
+
"style": "friendly"
156
+
}
157
+
]
158
+
```
150
159
151
160
We can control the format of the response that we get back, either by using the `Accept` header:
0 commit comments