• 图书管理系统后端接口


    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.BooleanField(default=False)

    1.2 urls.py 中设置路由

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

    1.3 book/views.py 视图函数

    
    
    from django.http import JsonResponse
    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"})

    2.测试接口

    2.1 测试获取所有图书接口

    http://127.0.0.1:8000/book/

    返回结果

    2.2 测试创建图书接口

    http://127.0.0.1:8000/book/
    .postman测试提交数据
    {

    "btitle":"红楼梦",
    "bpub_date":"2018-02-04",
    "bread": 100 ,
    "bcomment": 0
    }

    结果

    2.3 测试修改图书接口

    http://127.0.0.1:8000/books/
    postman测试提交数据
     
    {
    "id":5,
    "btitle":"红楼梦",
    "bpub_date":"2018-02-04",
    "bread":100,
    "bcomment":0
    }
     

    结果

    2.4 测试删除数据接口

    http://127.0.0.1:8000/books/
        postman测试提交数据
    { "id":15 }

    结果

  • 相关阅读:
    linux脚本mysql服务与keepalived服务高可用脚本
    linux脚本启动服务脚本
    linux脚本数据库备份脚本
    linux脚本清理日志文件脚本
    最大数字
    求阶乘
    素数
    水仙花数
    js基础1
    css常用属性2
  • 原文地址:https://www.cnblogs.com/shensy/p/13899360.html
Copyright © 2020-2023  润新知