DRF notes (1): Manual implementation of common APIs

Django REST Framework

Django itself is a framework that does not separate the front and back ends, which is suitable for many relatively simple development needs, but now many scenarios are more complicated, especially the front end is more complicated, and many front-end frameworks are very good now, which can greatly simplify the front-end development work. Sometimes it is necessary to separate the front and back ends; and now the development members in the general team are also separated from the front and back ends. The front end is dedicated to developing UI, and the back end is dedicated to developing business logic; The trend is already evident.
Django REST Framework is a front-end and back-end separation framework based on Django, which can encapsulate back-end functions into APIs to provide external services.

Manually implement the API

Although drf can implement the API well, writing it manually can help understand what drf does.
The common APIs are as follows:

method url action
GET /books/ query all records
POST /books/ add a record
GET /books/id query a record
PUT /books/id modify a record
DELETE /books/id delete a record

There is actually a PATCH to modify, which is more troublesome, so I won’t write it

The above summary is actually: add, delete, check and modify.
But there are two cases of checking: one is to check a specific piece of data (url ends with id), and the other is to check all data (url ends with resource name, such as /books)

The code related to this note is on the mannual-api branch
code repository: https://github.com/yexia553/drf

The project is based on an application that records information about books and characters in the books.
There are two tables, books and heroes.
The books table contains some book-related information, such as reading volume, publication time, etc.; the
heroes table contains some character-related information, such as gender, which book they belong to, and so on.

project structure

.
├── book---------------------Django application, test items are mainly in this directory
├── db.sqlite3--------- -----Database file
├── demo--------------------Django project directory
├── docs------------ --------note directory
├── manage.py
├── README.md
├── requirements.txt
├── test------------------ --Test code directory, some functions provide ready-made test code, just run it directly└──
venv

Code explanation (manual implementation of API)

There are two types of classification list view and detail view in the code.
Simply put, when the url ends with id, it will go to the detail view (processing a specific piece of data, such as /books/10, processing the 10th book, querying, modifying or deleting); when the url ends with Those ending with the resource name will go to the list view (such as /books/, query all books or create a new book, etc.).

  • list view /books/
class BookListView(View):
    """
    图书列表视图
    """
    def get(self, request):
        """查询所有图书"""
        books = BookInfo.objects.all()
        res = list()
        for book in books:
            bookinfo = {
    
    
                'id': book.id,
                'title': book.title,
                'pub_date': book.pub_date,
                'comment': book.comment,
                'read': book.read,
                'image': book.image.url if book.image else ''
            }
            res.append(bookinfo)
        return JsonResponse(res, safe=False)

    def post(self, request):
        '''新增一本图书'''
        body = request.body.decode()
        body = json.loads(body)
        book = BookInfo(
            title=body['title'],
            pub_date=body['pub_date'],
            comment=body['comment'],
            read=body['read']
        )
        book.save()
        res = {
    
    
            'id': book.id,
            'title': book.title,
            'pub_date': book.pub_date,
            'comment': book.comment,
            'read': book.read,
            'image': book.image.url if book.image else ''
        }
        # 返回新创建的图书的信息,以及201状态码
        return JsonResponse(res, status=201)
  • Detail view /books/id
class BookDetailView(View):
    """
    图书信息详情视图
    """
    def get(self, request, pk):
        '''查询某一本书的详细信息'''
        try:
            book = BookInfo.objects.get(id=pk)
        except BookInfo.DoesNotExist:
            return HttpResponse({
    
    'mesage':'查询的数据不存在'}, status=404)
        
        res = {
    
    
            'id': book.id,
            'title': book.title,
            'read': book.read,
            'pub_date': book.pub_date,
            'comment': book.comment,
            'image': book.image.url if book.image else ''
        }

        return JsonResponse(res)
    
    def put(self, request, pk):
        '''修改一本的数据'''
        # 先查询要修改的数据是否存在
        try:
            book = BookInfo.objects.get(id=pk)
        except BookInfo.DoesNotExist:
            return HttpResponse({
    
    'mesage':'要修改的数据不存在'}, status=404)
        # 获取请求中的数据
        body = request.body.decode()
        body = json.loads(body)
        # 修改数据并保存
        book.title=body['title'],
        book.pub_date=body['pub_date'],
        book.comment=body['comment'],
        book.read=body['read']
        book.save()
        # 构造返回数据
        res = {
    
    
            'id': book.id,
            'title': book.title,
            'read': book.read,
            'pub_date': book.pub_date,
            'comment': book.comment,
            'image': book.image.url if book.image else ''
        }
        return HttpResponse(res)

    def delete(self, request, pk):
        '''删除一本书'''
        try:
            book = BookInfo.objects.get(id=pk)
        except BookInfo.DoesNotExist:
            return HttpResponse({
    
    'mesage':'要删除的数据不存在'}, status=404)
        book.is_delete = True  # 逻辑删除
        book.save()
        # 删除可以没有响应体,只给出204状态码
        return HttpResponse(status=204)

The above two pieces of code, the list view and the detail view, are actually only common operations when using Django to do web development. In the View, the Model is used to add, delete, check and modify the database, but the data is finally returned instead of being rendered through the Template. Pages passed, which is very similar to DRF's API capabilities

  • url explained

Routing (urls.py under the demo path)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('books/', include('book.urls') )
]

Application routing (urls.py in the book directory)

urlpatterns = [
    url(r'^$', BookListView.as_view()),
    url(r'^(?P<pk>\d+)$', BookDetailView.as_view()),
]

According to the two routing codes, when accessing http://127.0.0.1:8080/books/, it will be processed by the list view (BookListView), and there are only two cases: querying all books and creating new books; when accessing http:// For 127.0.0.1:8080/books/ id , the request is processed by the detail view (BookDetailView). There are three situations: querying a specific book, modifying a book, and deleting a book.


  • There is a file under the test
    test path: mannual_api.py contains the test codes of POST and DELETE APIs, just run it directly, and it will return the prompt of test success or failure
$ python ./test/mannul_api.py 
资源创建成功,POST API测试成功
b'{"id": 10, "title": "\\u6597\\u7f57\\u5927\\u9646", "pub_date": "2015-12-12", "comment": "200", "read": "100", "image": ""}'
资源删除成功,DELETE API测试成功

Guess you like

Origin blog.csdn.net/u013117791/article/details/121974547