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.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 }

    结果

  • 相关阅读:
    138.安全退出的异常,要用throw 尽量不用exit(0)
    137.CPP自带异常
    136.异常的多态,父类对象,传递子类的引用或指针(地址)
    135.异常与类继承
    134.异常类的处理
    133.throw机制 抛出类类型
    132.try throw catch介绍
    CF1039D You Are Given a Tree
    CF576E Painting Edges
    【模板】并查集维护生成树
  • 原文地址:https://www.cnblogs.com/shensy/p/13899360.html
Copyright © 2011-2022 走看看