Skip to content

Commit 3f6004c

Browse files
authored
Use pk for URL conf and views. (encode#4592)
1 parent f9cf237 commit 3f6004c

6 files changed

+24
-24
lines changed

docs/tutorial/1-serialization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ Note that because we want to be able to POST to this view from clients that won'
259259
We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.
260260

261261
@csrf_exempt
262-
def snippet_detail(request, id):
262+
def snippet_detail(request, pk):
263263
"""
264264
Retrieve, update or delete a code snippet.
265265
"""
266266
try:
267-
snippet = Snippet.objects.get(id=id)
267+
snippet = Snippet.objects.get(pk=pk)
268268
except Snippet.DoesNotExist:
269269
return HttpResponse(status=404)
270270

@@ -291,7 +291,7 @@ Finally we need to wire these views up. Create the `snippets/urls.py` file:
291291

292292
urlpatterns = [
293293
url(r'^snippets/$', views.snippet_list),
294-
url(r'^snippets/(?P<id>[0-9]+)/$', views.snippet_detail),
294+
url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
295295
]
296296

297297
We also need to wire up the root urlconf, in the `tutorial/urls.py` file, to include our snippet app's URLs.

docs/tutorial/2-requests-and-responses.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ Our instance view is an improvement over the previous example. It's a little mo
6666
Here is the view for an individual snippet, in the `views.py` module.
6767

6868
@api_view(['GET', 'PUT', 'DELETE'])
69-
def snippet_detail(request, id):
69+
def snippet_detail(request, pk):
7070
"""
7171
Retrieve, update or delete a snippet instance.
7272
"""
7373
try:
74-
snippet = Snippet.objects.get(id=id)
74+
snippet = Snippet.objects.get(pk=pk)
7575
except Snippet.DoesNotExist:
7676
return Response(status=status.HTTP_404_NOT_FOUND)
7777

@@ -104,7 +104,7 @@ Start by adding a `format` keyword argument to both of the views, like so.
104104

105105
and
106106

107-
def snippet_detail(request, id, format=None):
107+
def snippet_detail(request, pk, format=None):
108108

109109
Now update the `urls.py` file slightly, to append a set of `format_suffix_patterns` in addition to the existing URLs.
110110

@@ -114,7 +114,7 @@ Now update the `urls.py` file slightly, to append a set of `format_suffix_patter
114114

115115
urlpatterns = [
116116
url(r'^snippets/$', views.snippet_list),
117-
url(r'^snippets/(?P<id>[0-9]+)$', views.snippet_detail),
117+
url(r'^snippets/(?P<pk>[0-9]+)$', views.snippet_detail),
118118
]
119119

120120
urlpatterns = format_suffix_patterns(urlpatterns)

docs/tutorial/3-class-based-views.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,27 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
3636
"""
3737
Retrieve, update or delete a snippet instance.
3838
"""
39-
def get_object(self, id):
39+
def get_object(self, pk):
4040
try:
41-
return Snippet.objects.get(id=id)
41+
return Snippet.objects.get(pk=pk)
4242
except Snippet.DoesNotExist:
4343
raise Http404
4444

45-
def get(self, request, id, format=None):
46-
snippet = self.get_object(id)
45+
def get(self, request, pk, format=None):
46+
snippet = self.get_object(pk)
4747
serializer = SnippetSerializer(snippet)
4848
return Response(serializer.data)
4949

50-
def put(self, request, id, format=None):
51-
snippet = self.get_object(id)
50+
def put(self, request, pk, format=None):
51+
snippet = self.get_object(pk)
5252
serializer = SnippetSerializer(snippet, data=request.data)
5353
if serializer.is_valid():
5454
serializer.save()
5555
return Response(serializer.data)
5656
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
5757

58-
def delete(self, request, id, format=None):
59-
snippet = self.get_object(id)
58+
def delete(self, request, pk, format=None):
59+
snippet = self.get_object(pk)
6060
snippet.delete()
6161
return Response(status=status.HTTP_204_NO_CONTENT)
6262

@@ -70,7 +70,7 @@ We'll also need to refactor our `urls.py` slightly now we're using class-based v
7070

7171
urlpatterns = [
7272
url(r'^snippets/$', views.SnippetList.as_view()),
73-
url(r'^snippets/(?P<id>[0-9]+)/$', views.SnippetDetail.as_view()),
73+
url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()),
7474
]
7575

7676
urlpatterns = format_suffix_patterns(urlpatterns)

docs/tutorial/4-authentication-and-permissions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Make sure to also import the `UserSerializer` class
8888
Finally we need to add those views into the API, by referencing them from the URL conf. Add the following to the patterns in `urls.py`.
8989

9090
url(r'^users/$', views.UserList.as_view()),
91-
url(r'^users/(?P<id>[0-9]+)/$', views.UserDetail.as_view()),
91+
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
9292

9393
## Associating Snippets with Users
9494

docs/tutorial/5-relationships-and-hyperlinked-apis.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ We'll add a url pattern for our new API root in `snippets/urls.py`:
4848

4949
And then add a url pattern for the snippet highlights:
5050

51-
url(r'^snippets/(?P<id>[0-9]+)/highlight/$', views.SnippetHighlight.as_view()),
51+
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', views.SnippetHighlight.as_view()),
5252

5353
## Hyperlinking our API
5454

@@ -116,16 +116,16 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
116116
url(r'^snippets/$',
117117
views.SnippetList.as_view(),
118118
name='snippet-list'),
119-
url(r'^snippets/(?P<id>[0-9]+)/$',
119+
url(r'^snippets/(?P<pk>[0-9]+)/$',
120120
views.SnippetDetail.as_view(),
121121
name='snippet-detail'),
122-
url(r'^snippets/(?P<id>[0-9]+)/highlight/$',
122+
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$',
123123
views.SnippetHighlight.as_view(),
124124
name='snippet-highlight'),
125125
url(r'^users/$',
126126
views.UserList.as_view(),
127127
name='user-list'),
128-
url(r'^users/(?P<id>[0-9]+)/$',
128+
url(r'^users/(?P<pk>[0-9]+)/$',
129129
views.UserDetail.as_view(),
130130
name='user-detail')
131131
])

docs/tutorial/6-viewsets-and-routers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ Now that we've bound our resources into concrete views, we can register the view
9292
urlpatterns = format_suffix_patterns([
9393
url(r'^$', api_root),
9494
url(r'^snippets/$', snippet_list, name='snippet-list'),
95-
url(r'^snippets/(?P<id>[0-9]+)/$', snippet_detail, name='snippet-detail'),
96-
url(r'^snippets/(?P<id>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
95+
url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'),
96+
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
9797
url(r'^users/$', user_list, name='user-list'),
98-
url(r'^users/(?P<id>[0-9]+)/$', user_detail, name='user-detail')
98+
url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail')
9999
])
100100

101101
## Using Routers

0 commit comments

Comments
 (0)