Django rest_framework--Getting Started Tutorial 3

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.