Rest_framwork's post and put
Rest_framwork can accept post and put delete get requests, you can make a just crud on resources, take a demo as an example,
1. Define a method in view first
@api_view ([' GET ', ' PUT ', ' delete ']) def book_detail (REQUEST,PK): "" "Retrieve, update or DELETE a book instance." " Try: Book = Book.objects.get (pk=pk) except Book.DoesNotExist:return Response (status=status. Http_404_not_found) If Request.method = = ' GET ': serializer = Bookserializer (book) return Response (Serializer.data) elif Request.method = = ' PUT ': serializer = Bookserializer (book, Data=request. DATA) if Serializer.is_valid (): serializer.save () return Response (serializer.data) Else: return Response (Serializer.errors, Status=status. http_400_bad_request) elif Request.method = = ' DELETE ': Book.delete () return Response (Status=status. Http_204_no_content)
Accept the book's primary key, if the primary key does not exist, will return 404 of the status code, you can use the Post object to update the object, this way is similar to in. NET, and then write a. NET similar. Of course, you can delete the object, where you can return the status code flexibly. If it is with curl, because in the setting is configured to add-u test:pwd this authentication in Firefox is not used, also please note that you can post one in the GUI, copy Curl command placed in the terminal execution, this time is OK. Here the basic curd will be finished next see can write some Rest_framework API, interested students can follow, write the text when recorded
Django rest_framework--Getting Started Tutorial 3