mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Use pk for URL conf and views. (#4592)
This commit is contained in:
		
							parent
							
								
									f9cf237e0b
								
							
						
					
					
						commit
						3f6004c5a9
					
				| 
						 | 
					@ -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:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
        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')
 | 
				
			||||||
    ])
 | 
					    ])
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user