This is the second day of my participation in Gwen Challenge

What is front end separation? Why do we separate the front and back ends?

  • The back end returns only data: When the front and back ends are separated, the back end returns only the data required by the front end and does not render HTML pages or control the front end effect.
  • Advantages: Different front ends (WEB/APP/…) The same data can be retrieved from a unified back end for different processing and presentation. It is conducive to the division of labor development at the front and back ends, and also conducive to the subsequent expansion of the front-end mode.
  • Back-end API interface implementations (requests, paths, returns, etc.) can vary in style, and following a RESTful design style helps to unify the style (recommended rather than mandatory)

RESTful API request

(Brief, not complete)

  • Using the GET/POST/PUT/DELETE request corresponding to the SELECT/CREATE/UPDATE/DELETE (check/add/change/DELETE) of database operations
  • Return JSON data

Implementation steps

  1. Set view: Set methods of the same name for different requests (GET/POST/PUT/DELETE)
    1. Query the corresponding object based on the parameters extracted from the address
    2. Perform operations based on different requests and the information in the request body
    3. Return JSON data
  2. Add route: Add the corresponding IP address and set the parameters to be retrieved

1. Set the view (views.py)

# To implement a book search (get), (put), (delete) REST interface, feedback JSON data
class BookAPIView(View) :
    # query
    def get(self, request, pk) :
        GET /books/
      
       / ""
      
        # fetch the object of the corresponding book according to the PK value passed in
        try:
            book = BookInfo.objects.get(pk=pk)
        except BookInfo.DoesNotExist:
            return HttpResponse(status=404)
        # return JSON data
        return JsonResponse({
            'id': book.id.'btitle': book.btitle,
        })
    
    # modified
    def put(self, request, pk) :
        """ Modify book information routing: PUT /books/< PK > ""
        # fetch the object of the corresponding book according to the PK value passed in
        try:
            book = BookInfo.objects.get(pk=pk)
        except BookInfo.DoesNotExist:
            return HttpResponse(status=404)
        
        Convert the JSON format in the request to dictionary format
        json_bytes = request.body
        json_str = json_bytes.decode()
        book_dict = json.loads(json_str)

        # check parameters (omitted)
        Update the object and save it
        book.btitle = book_dict.get('btitle')
        book.save()

        # return JSON data
        return JsonResponse({
            'id': book.id.'btitle': book.btitle,
        })

    # remove
    def delete(self, request, pk) :
        """ DELETE book route: DELETE /books/
      
       / ""
      
        # fetch the object of the corresponding book according to the PK value passed in
        try:
            book = BookInfo.objects.get(pk=pk)
        except BookInfo.DoesNotExist:
            return HttpResponse(status=404)
        # delete object
        book.delete()

        # return operation successful (204)
        return HttpResponse(status=204)
Copy the code

2. Add a route (urls.py)

urlpatterns = [
    url(r'^books/(? P
      
       \d+)/$'
      , views.BookAPIView.as_view())
]
Copy the code