I am trying to make a CRUD API using the Django Rest Framework.
Here is my urls.py
router = routers.DefaultRouter()
router.register('products', ProductView)
# router.register('products/delete/<id>', ProductView)
(This is not working)
urlpatterns = [
path('', include(router.urls)),
]
Here is my views.py
class ProductView(viewsets.ModelViewSet):
queryset = Products.objects.all()
serializer_class = ProductSerializer
By using the above code I can fetch a list of all products ---
1) localhost/products (This gives a list of all products)
2) localhost/products/1 (This gives a product with id 1)
How to use other URLs for operations such as Delete, Update etc.
This above code is working. Nothing is wrong . I Just want to know how to use other routes from the client.
By this configuration, you will have two URLs
localhost/products/
localhost/products/<product_pk>/
But, the difference is which method is used to interact with the API . I hope you already know we have got POST
, PUT
, PATCH
, DELETE
etc HTTP methods and these above APIs are capable of those HTTP methods.
You can see the entire URL configuration (as table) in DRF doc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.