Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
3f6004c5
Commit
3f6004c5
authored
Oct 20, 2016
by
Tom Christie
Committed by
GitHub
Oct 20, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use pk for URL conf and views. (#4592)
parent
f9cf237e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
24 additions
and
24 deletions
+24
-24
docs/tutorial/1-serialization.md
+3
-3
docs/tutorial/2-requests-and-responses.md
+4
-4
docs/tutorial/3-class-based-views.md
+9
-9
docs/tutorial/4-authentication-and-permissions.md
+1
-1
docs/tutorial/5-relationships-and-hyperlinked-apis.md
+4
-4
docs/tutorial/6-viewsets-and-routers.md
+3
-3
No files found.
docs/tutorial/1-serialization.md
View file @
3f6004c5
...
@@ -259,12 +259,12 @@ Note that because we want to be able to POST to this view from clients that won'
...
@@ -259,12 +259,12 @@ Note that because we want to be able to POST to this view from clients that won'
We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.
We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.
@csrf_exempt
@csrf_exempt
def snippet_detail(request,
id
):
def snippet_detail(request,
pk
):
"""
"""
Retrieve, update or delete a code snippet.
Retrieve, update or delete a code snippet.
"""
"""
try:
try:
snippet = Snippet.objects.get(
id=id
)
snippet = Snippet.objects.get(
pk=pk
)
except Snippet.DoesNotExist:
except Snippet.DoesNotExist:
return HttpResponse(status=404)
return HttpResponse(status=404)
...
@@ -291,7 +291,7 @@ Finally we need to wire these views up. Create the `snippets/urls.py` file:
...
@@ -291,7 +291,7 @@ Finally we need to wire these views up. Create the `snippets/urls.py` file:
urlpatterns = [
urlpatterns = [
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/(?P<
id
>[0-9]+)/$', views.snippet_detail),
url(r'^snippets/(?P<
pk
>[0-9]+)/$', views.snippet_detail),
]
]
We also need to wire up the root urlconf, in the
`tutorial/urls.py`
file, to include our snippet app's URLs.
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
View file @
3f6004c5
...
@@ -66,12 +66,12 @@ Our instance view is an improvement over the previous example. It's a little mo
...
@@ -66,12 +66,12 @@ Our instance view is an improvement over the previous example. It's a little mo
Here is the view for an individual snippet, in the
`views.py`
module.
Here is the view for an individual snippet, in the
`views.py`
module.
@api_view(['GET', 'PUT', 'DELETE'])
@api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request,
id
):
def snippet_detail(request,
pk
):
"""
"""
Retrieve, update or delete a snippet instance.
Retrieve, update or delete a snippet instance.
"""
"""
try:
try:
snippet = Snippet.objects.get(
id=id
)
snippet = Snippet.objects.get(
pk=pk
)
except Snippet.DoesNotExist:
except Snippet.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(status=status.HTTP_404_NOT_FOUND)
...
@@ -104,7 +104,7 @@ Start by adding a `format` keyword argument to both of the views, like so.
...
@@ -104,7 +104,7 @@ Start by adding a `format` keyword argument to both of the views, like so.
and
and
def snippet_detail(request,
id
, format=None):
def snippet_detail(request,
pk
, format=None):
Now update the
`urls.py`
file slightly, to append a set of
`format_suffix_patterns`
in addition to the existing URLs.
Now update the
`urls.py`
file slightly, to append a set of
`format_suffix_patterns`
in addition to the existing URLs.
...
@@ -114,7 +114,7 @@ Now update the `urls.py` file slightly, to append a set of `format_suffix_patter
...
@@ -114,7 +114,7 @@ Now update the `urls.py` file slightly, to append a set of `format_suffix_patter
urlpatterns = [
urlpatterns = [
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/(?P<
id
>[0-9]+)$', views.snippet_detail),
url(r'^snippets/(?P<
pk
>[0-9]+)$', views.snippet_detail),
]
]
urlpatterns = format_suffix_patterns(urlpatterns)
urlpatterns = format_suffix_patterns(urlpatterns)
...
...
docs/tutorial/3-class-based-views.md
View file @
3f6004c5
...
@@ -36,27 +36,27 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
...
@@ -36,27 +36,27 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
"""
"""
Retrieve, update or delete a snippet instance.
Retrieve, update or delete a snippet instance.
"""
"""
def get_object(self,
id
):
def get_object(self,
pk
):
try:
try:
return Snippet.objects.get(
id=id
)
return Snippet.objects.get(
pk=pk
)
except Snippet.DoesNotExist:
except Snippet.DoesNotExist:
raise Http404
raise Http404
def get(self, request,
id
, format=None):
def get(self, request,
pk
, format=None):
snippet = self.get_object(
id
)
snippet = self.get_object(
pk
)
serializer = SnippetSerializer(snippet)
serializer = SnippetSerializer(snippet)
return Response(serializer.data)
return Response(serializer.data)
def put(self, request,
id
, format=None):
def put(self, request,
pk
, format=None):
snippet = self.get_object(
id
)
snippet = self.get_object(
pk
)
serializer = SnippetSerializer(snippet, data=request.data)
serializer = SnippetSerializer(snippet, data=request.data)
if serializer.is_valid():
if serializer.is_valid():
serializer.save()
serializer.save()
return Response(serializer.data)
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request,
id
, format=None):
def delete(self, request,
pk
, format=None):
snippet = self.get_object(
id
)
snippet = self.get_object(
pk
)
snippet.delete()
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
...
@@ -70,7 +70,7 @@ We'll also need to refactor our `urls.py` slightly now we're using class-based v
...
@@ -70,7 +70,7 @@ We'll also need to refactor our `urls.py` slightly now we're using class-based v
urlpatterns = [
urlpatterns = [
url(r'^snippets/$', views.SnippetList.as_view()),
url(r'^snippets/$', views.SnippetList.as_view()),
url(r'^snippets/(?P<
id
>[0-9]+)/$', views.SnippetDetail.as_view()),
url(r'^snippets/(?P<
pk
>[0-9]+)/$', views.SnippetDetail.as_view()),
]
]
urlpatterns = format_suffix_patterns(urlpatterns)
urlpatterns = format_suffix_patterns(urlpatterns)
...
...
docs/tutorial/4-authentication-and-permissions.md
View file @
3f6004c5
...
@@ -88,7 +88,7 @@ Make sure to also import the `UserSerializer` class
...
@@ -88,7 +88,7 @@ Make sure to also import the `UserSerializer` class
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`
.
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`
.
url(r'^users/$', views.UserList.as_view()),
url(r'^users/$', views.UserList.as_view()),
url(r'^users/(?P<
id
>[0-9]+)/$', views.UserDetail.as_view()),
url(r'^users/(?P<
pk
>[0-9]+)/$', views.UserDetail.as_view()),
## Associating Snippets with Users
## Associating Snippets with Users
...
...
docs/tutorial/5-relationships-and-hyperlinked-apis.md
View file @
3f6004c5
...
@@ -48,7 +48,7 @@ We'll add a url pattern for our new API root in `snippets/urls.py`:
...
@@ -48,7 +48,7 @@ We'll add a url pattern for our new API root in `snippets/urls.py`:
And then add a url pattern for the snippet highlights:
And then add a url pattern for the snippet highlights:
url(r'^snippets/(?P<
id
>[0-9]+)/highlight/$', views.SnippetHighlight.as_view()),
url(r'^snippets/(?P<
pk
>[0-9]+)/highlight/$', views.SnippetHighlight.as_view()),
## Hyperlinking our API
## Hyperlinking our API
...
@@ -116,16 +116,16 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
...
@@ -116,16 +116,16 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
url(r'^snippets/$',
url(r'^snippets/$',
views.SnippetList.as_view(),
views.SnippetList.as_view(),
name='snippet-list'),
name='snippet-list'),
url(r'^snippets/(?P<
id
>[0-9]+)/$',
url(r'^snippets/(?P<
pk
>[0-9]+)/$',
views.SnippetDetail.as_view(),
views.SnippetDetail.as_view(),
name='snippet-detail'),
name='snippet-detail'),
url(r'^snippets/(?P<
id
>[0-9]+)/highlight/$',
url(r'^snippets/(?P<
pk
>[0-9]+)/highlight/$',
views.SnippetHighlight.as_view(),
views.SnippetHighlight.as_view(),
name='snippet-highlight'),
name='snippet-highlight'),
url(r'^users/$',
url(r'^users/$',
views.UserList.as_view(),
views.UserList.as_view(),
name='user-list'),
name='user-list'),
url(r'^users/(?P<
id
>[0-9]+)/$',
url(r'^users/(?P<
pk
>[0-9]+)/$',
views.UserDetail.as_view(),
views.UserDetail.as_view(),
name='user-detail')
name='user-detail')
])
])
...
...
docs/tutorial/6-viewsets-and-routers.md
View file @
3f6004c5
...
@@ -92,10 +92,10 @@ Now that we've bound our resources into concrete views, we can register the view
...
@@ -92,10 +92,10 @@ Now that we've bound our resources into concrete views, we can register the view
urlpatterns = format_suffix_patterns([
urlpatterns = format_suffix_patterns([
url(r'^$', api_root),
url(r'^$', api_root),
url(r'^snippets/$', snippet_list, name='snippet-list'),
url(r'^snippets/$', snippet_list, name='snippet-list'),
url(r'^snippets/(?P<
id
>[0-9]+)/$', snippet_detail, name='snippet-detail'),
url(r'^snippets/(?P<
pk
>[0-9]+)/$', snippet_detail, name='snippet-detail'),
url(r'^snippets/(?P<
id
>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
url(r'^snippets/(?P<
pk
>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
url(r'^users/$', user_list, name='user-list'),
url(r'^users/$', user_list, name='user-list'),
url(r'^users/(?P<
id
>[0-9]+)/$', user_detail, name='user-detail')
url(r'^users/(?P<
pk
>[0-9]+)/$', user_detail, name='user-detail')
])
])
## Using Routers
## Using Routers
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment