当前位置: 首页 > news >正文

Django之一个基于多表的图书管理系统

准备工作参考单表,这里只贴出关键代码,项目结构与基于单表的木有区别.

1.url控制器

urls.py

 
  1: from django.contrib import admin
  2: from django.urls import path,re_path
  3: from app01 import views
  4: 
  5: urlpatterns = [
  6:     path('admin/', admin.site.urls),
  7:     path('book/add', views.add_book),
  8:     path('book/search', views.search_book),
  9:     re_path('book/(\d+)/change', views.change_book),
 10:     re_path('book/(\d+)/delete', views.delete_book),
 11: ]

2.模型层

models.py


 
  1: from django.db import models
  2: # Create your models here.
  3: class Author(models.Model):
  4:     nid = models.AutoField(primary_key=True)
  5:     name = models.CharField(max_length=32)
  6:     age = models.IntegerField()
  7: class Publish(models.Model):
  8:     nid = models.AutoField(primary_key=True)
  9:     name = models.CharField(max_length=32)
 10:     city = models.CharField(max_length=32)
 11:     email = models.EmailField()
 12: class Book(models.Model):
 13:     nid = models.AutoField(primary_key=True)
 14:     title = models.CharField(max_length=32)
 15:     publishDate = models.DateField()
 16:     price = models.DecimalField(max_digits=5, decimal_places=2)
 17:     publish = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE)
 18:     authors = models.ManyToManyField(to="Author",)
 19: 

3.视图层

views.py


 
  1: from django.shortcuts import render, HttpResponse, redirect
  2: 
  3: # Create your views here.
  4: from .models import Publish, Author, Book
  5: 
  6: 
  7: def add_book(request):
  8: 
  9:     if request.method == "POST":
 10:         title = request.POST.get("title")
 11:         price = request.POST.get("price")
 12:         pub_date = request.POST.get("pub_date")
 13:         publish_id = request.POST.get("publish_id")
 14:         authors_id_list = request.POST.getlist("authors_id_list")
 15:         # print(authors_id_list)
 16: 
 17:         book_obj = Book.objects.create(title=title, price=price, publishDate=pub_date, publish_id=publish_id)
 18:         book_obj.authors.add(*authors_id_list)
 19: 
 20:         return redirect("/book/search")
 21: 
 22:     publish_list = Publish.objects.all()
 23:     author_list = Author.objects.all()
 24:     return render(request, "add_book.html", {"publish_list": publish_list, "author_list": author_list})
 25: 
 26: 
 27: def search_book(request):
 28: 
 29:     book_list = Book.objects.all()
 30: 
 31:     return render(request, "search_book.html", {"book_list": book_list})
 32: 
 33: 
 34: def change_book(request, edit_book_id):
 35: 
 36:     edit_book_obj = Book.objects.filter(pk=edit_book_id)[0]
 37:     if request.method == "POST":
 38:         title = request.POST.get("title")
 39:         price = request.POST.get("price")
 40:         pub_date = request.POST.get("pub_date")
 41:         publish_id = request.POST.get("publish_id")
 42:         authors_id_list = request.POST.getlist("authors_id_list")
 43: 
 44:         Book.objects.filter(pk=edit_book_id).update(title=title, price=price, publishDate=pub_date, publish_id=publish_id)
 45:         # edit_book_obj.authors.clear()
 46:         # edit_book_obj.authors.add(*authors_id_list)
 47:         # 同上的写法
 48:         edit_book_obj.authors.set(authors_id_list)
 49: 
 50:         return redirect("/book/search")
 51: 
 52:     publish_list = Publish.objects.all()
 53:     author_list = Author.objects.all()
 54:     return render(request, "editbook.html", {"edit_book_obj": edit_book_obj, "publish_list": publish_list, "author_list": author_list})
 55: 
 56: 
 57: def delete_book(request, delete_book_id):
 58: 
 59:     Book.objects.filter(pk=delete_book_id).delete()
 60: 
 61:     return redirect("/book/search")

4.模板层

templates

add_book.html


 
  1: <!DOCTYPE html>
  2: <html lang="en">
  3: <head>
  4:     <meta charset="UTF-8">
  5:     <title>添加页面</title>
  6:     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  7: </head>
  8: <body>
  9: <div>
 10:     <h3>添加书籍</h3>
 11: </div>
 12: 
 13: <div class="container">
 14:     <div class="row">
 15:         <div class="col-md-6 col-md-offset-3">
 16:             <form action="" method="post">
 17:                 {% csrf_token %}
 18:                 <div class="from-group">
 19:                     <label for="">名称</label>
 20:                     <input type="text" name="title" class="form-control">
 21:                 </div>
 22:                 <div class="from-group">
 23:                     <label for="">价格</label>
 24:                     <input type="text" name="price" class="form-control">
 25:                 </div>
 26:                 <div class="from-group">
 27:                     <label for="">出版日期</label>
 28:                     <input type="date" name="pub_date" class="form-control">
 29:                 </div>
 30:                 <div class="from-group">
 31:                     <label for="">出版社</label>
 32:                     <select name="publish_id" id="" class="form-control">
 33:                         {% for publish in publish_list %}
 34:                             <option value="{{ publish.pk }}">{{ publish.name }}</option>
 35:                         {% endfor %}
 36:                     </select>
 37:                 </div>
 38:                 <div class="from-group">
 39:                     <label for="">作者</label>
 40:                     <select type="text" name="authors_id_list" id="" multiple class="form-control">
 41:                         {% for author in author_list %}
 42:                             <option value="{{ author.pk }}">{{ author.name }}</option>
 43:                         {% endfor %}
 44:                     </select>
 45:                 </div>
 46:                 <div>
 47:                     <input type="submit" class="btn btn-default">
 48:                 </div>
 49:             </form>
 50:         </div>
 51:     </div>
 52: </div>
 53: </body>
 54: </html>

editbook.html


 
  1: <!DOCTYPE html>
  2: <html lang="en">
  3: <head>
  4:     <meta charset="UTF-8">
  5:     <title>添加页面</title>
  6:     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  7: </head>
  8: <body>
  9: <div>
 10:     <h3>编辑书籍</h3>
 11: </div>
 12: 
 13: <div class="container">
 14:     <div class="row">
 15:         <div class="col-md-6 col-md-offset-3">
 16:             <form action="" method="post">
 17:                 {% csrf_token %}
 18:                 <div class="from-group">
 19:                     <label for="">名称</label>
 20:                     <input type="text" name="title" class="form-control" value="{{ edit_book_obj.title }}">
 21:                 </div>
 22:                 <div class="from-group">
 23:                     <label for="">价格</label>
 24:                     <input type="text" name="price" class="form-control" value="{{ edit_book_obj.price }}">
 25:                 </div>
 26:                 <div class="from-group">
 27:                     <label for="">出版日期</label>
 28:                     <input type="date" name="pub_date" class="form-control" value="{{ edit_book_obj.publishDate|date:"Y-m-d" }}">
 29:                 </div>
 30:                 <div class="from-group">
 31:                     <label for="">出版社</label>
 32:                     <select name="publish_id" id="" class="form-control">
 33:                         {% for publish in publish_list %}
 34:                             {% if edit_book_obj.publish == publish %}
 35:                                 <option selected value="{{ publish.pk }}">{{ publish.name }}</option>
 36:                             {% else %}
 37:                                 <option value="{{ publish.pk }}">{{ publish.name }}</option>
 38:                             {% endif %}
 39:                         {% endfor %}
 40:                     </select>
 41:                 </div>
 42:                 <div class="from-group">
 43:                     <label for="">作者</label>
 44:                     <select type="text" name="authors_id_list" id="" multiple class="form-control">
 45:                         {% for author in author_list %}
 46:                             {% if author in edit_book_obj.author.all %}
 47:                                 <option selected value="{{ author.pk }}">{{ author.name }}</option>
 48:                             {% else %}
 49:                                 <option value="{{ author.pk }}">{{ author.name }}</option>
 50:                             {% endif %}
 51:                         {% endfor %}
 52:                     </select>
 53:                 </div>
 54:                 <div>
 55:                     <input type="submit" class="btn btn-default">
 56:                 </div>
 57:             </form>
 58:         </div>
 59:     </div>
 60: </div>
 61: </body>
 62: </html>

search_book.html


 
  1: <!DOCTYPE html>
  2: <html lang="en">
  3: <head>
  4:     <meta charset="UTF-8">
  5:     <title>查看页面</title>
  6:     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  7: </head>
  8: <body>
  9: <div>
 10:     <h3>查看书籍</h3>
 11: </div>
 12: <div>
 13:     <h3>添加书籍</h3>
 14:     <a href="/book/add" class="btn btn-info">添加</a>
 15: </div>
 16: <div class="container">
 17:     <div class="row">
 18:         <div class="col-md-8 col-md-offset-3">
 19:             <table class="table table-bordered table-hover table-striped" >
 20:                 <thead>
 21:                     <tr>
 22:                         <th>编号</th>
 23:                         <th>书籍名称</th>
 24:                         <th>价格</th>
 25:                         <th>出版日期</th>
 26:                         <th>出版社</th>
 27:                         <th>作者</th>
 28:                         <th>操作</th>
 29:                     </tr>
 30:                 </thead>
 31:                 <tbody>
 32:                     {% for book in book_list %}
 33:                     <tr>
 34:                         <td>{{ forloop.counter }}</td>
 35:                         <td>{{ book.title }}</td>
 36:                         <td>{{ book.price }}</td>
 37:                         <td>{{ book.publishDate|date:"Y-m-d" }}</td>
 38:                         <td>
 39:                             {{ book.publish.name }}
 40:                         </td>
 41:                         <td>
 42:                             {% for author in book.authors.all %}
 43:                                 {% if forloop.last %}
 44:                                     <span>{{ author.name }}</span>
 45:                                 {% else %}
 46:                                     <span>{{ author.name }}.</span>
 47:                                 {% endif %}
 48:                             {% endfor %}
 49:                         </td>
 50:                         <td>
 51:                             <a href="/book/{{ book.pk }}/change" class="btn btn-warning">编辑</a>
 52:                             <a href="/book/{{ book.pk }}/delete" class="btn btn-danger">删除</a>
 53:                         </td>
 54:                     </tr>
 55:                     {% endfor %}
 56:                 </tbody>
 57:             </table>
 58:         </div>
 59:     </div>
 60: </div>
 61: </body>
 62: </html>

转载于:https://www.cnblogs.com/haoqirui/p/10176586.html

相关文章:

  • 大学排名爬取 + 绘制树状图 + 绘制圆饼图
  • 一封支付宝老员工的离职信
  • spark的广播变量
  • regsvr32.exe使用详解
  • openwrt下定义软件包的依赖关系类型
  • 找不到IIS Out-Of-Process Pooled Applications
  • 注册表:DWORD
  • 如何提取hyper-v的驱动程序
  • js base64 转成图片上传
  • 超像素、语义分割、实例分割、全景分割 傻傻分不清?
  • webpack压缩图片之项目资源优化
  • 内存释放
  • 我的译作
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • 百度排名公式最新版
  • [译]前端离线指南(上)
  • java8 Stream Pipelines 浅析
  • Joomla 2.x, 3.x useful code cheatsheet
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • Js基础——数据类型之Null和Undefined
  • PHP CLI应用的调试原理
  • Python进阶细节
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • spring boot 整合mybatis 无法输出sql的问题
  • supervisor 永不挂掉的进程 安装以及使用
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • 阿里云前端周刊 - 第 26 期
  • 工作手记之html2canvas使用概述
  • 缓存与缓冲
  • 聊聊redis的数据结构的应用
  • 前端面试总结(at, md)
  • 区块链共识机制优缺点对比都是什么
  • 为视图添加丝滑的水波纹
  • 项目实战-Api的解决方案
  • 运行时添加log4j2的appender
  • 正则与JS中的正则
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​低代码平台的核心价值与优势
  • (2)nginx 安装、启停
  • (4.10~4.16)
  • (c语言)strcpy函数用法
  • (第一天)包装对象、作用域、创建对象
  • (二十三)Flask之高频面试点
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)一些感悟
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .desktop 桌面快捷_Linux桌面环境那么多,这几款优秀的任你选
  • .NET Framework与.NET Framework SDK有什么不同?