syl----图书管理系统后端

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

books/models.py 中设置表

class Book(models.Model):
    btitle=models.CharField(max_length=32)
    bpub_date=models.DateField()
    bread=models.IntegerField()
    bcomment=models.IntegerField()
    is_delete=models.BooleanField(default=False)

    class Meta:
        db_table='Book'

urls.py 中设置路由

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

book/views.py 视图函数

class BooksView(View):
    #查
    def get(self,request):
        books=Book.objects.filter(is_delete=False)
        book_list=books.values('id','btitle','bpub_date','bread','bcomment')
        book_list=list(book_list)
        data={
            'code':0,
            'msg':'ok',
            '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=Book(btitle=btitle,bpub_date=bpub_date,bread=bread,bcomment=bcomment)
        book.save()
        return JsonResponse({'code':200,'msg':'ok'})
    #改
    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=Book.objects.get(id=id)
        book.btitle=btitle
        book.bpub_date=bpub_date
        book.bread=bread
        book.bcomment=bcomment
        book.save()
        return JsonResponse({'code':200,'msg':'ok'})
    #删
    def delete(self,request):
        body_json=request.body.decode()
        body_dict=json.loads(body_json)
        id=body_dict.get('id')
        book=Book.objects.get(id=id)
        book.is_delete=True
        book.save()
        return JsonResponse({'code':200,'msg':'ok'})

测试接口 查

http://127.0.0.1:8000/book/

{
    "code": 0,
    "msg": "ok",
    "books": [
        {
            "id": 2,
            "btitle": "红楼梦",
            "bpub_date": "2018-02-04",
            "bread": 100,
            "bcomment": 0
        },
        {
            "id": 3,
            "btitle": "遮天",
            "bpub_date": "2018-02-04",
            "bread": 100,
            "bcomment": 0
        }
    ]
}

测试创建图书接口

http://127.0.0.1:8000/book/

{  "btitle":"遮天", "bpub_date":"2018-02-04", "bread":100, "bcomment":0 }

syl----图书管理系统后端

测试修改图书接口

http://127.0.0.1:8000/book/

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

syl----图书管理系统后端

测试删除数据接口

http://127.0.0.1:8000/book/

{  "id":5 }

syl----图书管理系统后端

上一篇:04.增加media文件配置


下一篇:基于Express+xterm.js的WebSSH