vue製作toast元件npm包示例程式碼
阿新 • • 發佈:2020-10-29
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(defaultbooks/models.py=False) # 是否刪除
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 importbook/views.py* 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/books/book/