like title,
what\'s different about list_route
and detail_route
in django-rest-framework?
if I want to get 1
in url xxx/book
@list_route
and @detail_route
are extra actions that we can add to a ViewSet
. Both provide a custom routing facility in view set. Any methods on the ViewSet
decorated with @detail_route
or @list_route
will also be routed. list_route
will give all the related records, whereas detail_route
will provide only a particular record. For example, given a method like this on the UserViewSet class:
class UserViewSet(ModelViewSet):
...
@detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf])
def set_password(self, request, pk=None):
The following URL pattern would additionally be generated:
URL pattern: ^users/{pk}/set_password/$ Name: 'user-set-password'
For more information on routers you can visit the official Django Rest Rramewrok documentation on routers.
If you want get xxx/books/1/
then your url.py
and views.py
should look like this.
urls.py
:
url(r'^xxx/books/(?P[0-9]+)$', views.myview)
views.py
:
@csrf_exempt
def myview(request , id):