zoukankan      html  css  js  c++  java
  • 图书管理系统后端接口

    1.图书管理系统后端接口

    1.1 books/models.py 中设置表

    from django.db import models
    
    class Books(models.Model):
        btitle = models.CharField(max_length=20)  # 标题
        bpub_date = models.DateField()  # 日期
        bread = models.IntegerField()  # 阅读
        bcomment = models.IntegerField()  # 评论
        is_delete = models.BigIntegerField(default=False) # 是否删除
    books/models.py

     1.2 urls.py 中设置路由

    from django.urls import path
    
    from books import views
    
    urlpatterns = [
    
        path('book/', views.BooksView.as_view()),
    ]
    urls.py

     1.3 book/views.py 视图函数

    import json
    
    from django.http import JsonResponse
    from rest_framework.views import View
    
    from .models import *
    
    
    class BooksView(View):
        def get(self, request):
            books = Books.objects.filter(is_delete=False)
            book_list = books.values('id', 'btitle', 'bpub_date', 'bread', 'bcomment')
            book_list = list(book_list)
            data = {
                "code": 0,
                "msg": "success",
                "books": book_list
            }
            return JsonResponse(data)
    
        def post(self, request):
            body_json = request.body.decode()
            body_dict = json.loads(body_json)
            btitle = body_dict.get('btitle')
            bpub_date = body_dict.get('bpub_date')
            bread = body_dict.get('bread')
            bcomment = body_dict.get('bcomment')
    
            book = Books(btitle=btitle, bpub_date=bpub_date, bread=bread, bcomment=bcomment)
            book.save()
            return JsonResponse({"code": 0, "msg": "success"})
    
        def put(self, request):
            body_json = request.body.decode()
            body_dict = json.loads(body_json)
            id = body_dict.get('id')
            btitle = body_dict.get('btitle')
            bpub_date = body_dict.get('bpub_date')
            bread = body_dict.get('bread')
            bcomment = body_dict.get('bcomment')
    
            book = Books.objects.get(id=id)
            book.btitle = btitle
            book.bpub_date = bpub_date
            book.bread = bread
            book.bcomment = bcomment
            book.save()
            return JsonResponse({"code": 0, "msg": "success"})
    
        def delete(self, request):
            body_json = request.body.decode()
            body_dict = json.loads(body_json)
            id = body_dict.get('id')
    
            book = Books.objects.get(id=id)
            book.is_delete = True
            book.save()
            return JsonResponse({"code": 0, "msg": "success"})
    book/views.py

     2.测试接口

    2.1 测试获取所有图书接口

    http://127.0.0.1:8000/books/book/

     

     

     

  • 相关阅读:
    信号量整理
    Linux下串口配置初步探寻
    Vmware启动ubuntu 出现错误。
    软件工程结课作业
    第四次博客作业-结对项目
    20194731 软件工程第三次作业——博文软件质量保障初探
    20194731 第一次作业-博客初体验
    20194731 自动生成四则运算题第一版报告
    误删除 mySQL 用户解决办法
    mySQL的安装过程---二进制和源码安装
  • 原文地址:https://www.cnblogs.com/pythonqwertytre/p/13899519.html
Copyright © 2011-2022 走看看