基于python+django+mysql的图书信息管理系统 代码

时间: 2023-07-30 22:10:24 浏览: 79
以下是一个基于 Python + Django + MySQL 的图书信息管理系统的代码示例: 1. 创建 Django 项目 ```bash django-admin startproject book_management_system ``` 2. 创建 Django 应用 ```bash cd book_management_system python manage.py startapp book ``` 3. 配置 MySQL 数据库连接 在 `book_management_system/settings.py` 文件中添加以下内容: ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'book_db', 'USER': 'root', 'PASSWORD': 'password', 'HOST': '127.0.0.1', 'PORT': '3306', } } ``` 其中,`book_db` 是数据库名称,`root` 和 `password` 分别是 MySQL 的用户名和密码。 4. 创建图书信息模型 在 `book/models.py` 文件中添加以下内容: ```python from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=50) publisher = models.CharField(max_length=50) pub_date = models.DateField() price = models.DecimalField(max_digits=10, decimal_places=2) create_time = models.DateTimeField(auto_now_add=True) update_time = models.DateTimeField(auto_now=True) def __str__(self): return self.title ``` 其中,`Book` 是图书信息模型,包含了图书的标题、作者、出版社、出版日期、价格、创建时间和更新时间等字段。 5. 创建图书信息视图 在 `book/views.py` 文件中添加以下内容: ```python from django.shortcuts import render from django.http import HttpResponseRedirect from .models import Book def book_list(request): books = Book.objects.all() return render(request, 'book_list.html', {'books': books}) def book_detail(request, book_id): book = Book.objects.get(id=book_id) return render(request, 'book_detail.html', {'book': book}) def book_create(request): if request.method == 'POST': title = request.POST.get('title') author = request.POST.get('author') publisher = request.POST.get('publisher') pub_date = request.POST.get('pub_date') price = request.POST.get('price') Book.objects.create(title=title, author=author, publisher=publisher, pub_date=pub_date, price=price) return HttpResponseRedirect('/book/') else: return render(request, 'book_create.html') def book_update(request, book_id): book = Book.objects.get(id=book_id) if request.method == 'POST': book.title = request.POST.get('title') book.author = request.POST.get('author') book.publisher = request.POST.get('publisher') book.pub_date = request.POST.get('pub_date') book.price = request.POST.get('price') book.save() return HttpResponseRedirect('/book/') else: return render(request, 'book_update.html', {'book': book}) def book_delete(request, book_id): book = Book.objects.get(id=book_id) book.delete() return HttpResponseRedirect('/book/') ``` 其中,`book_list` 函数用于列出所有图书信息,`book_detail` 函数用于查看单个图书信息,`book_create` 函数用于创建新的图书信息,`book_update` 函数用于更新已有的图书信息,`book_delete` 函数用于删除图书信息。 6. 创建图书信息模板 在 `book/templates/` 目录下创建以下模板文件: `book_list.html`: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书信息管理系统</title> </head> <body> <h1>图书信息列表</h1> <table> <thead> <tr> <th>ID</th> <th>标题</th> <th>作者</th> <th>出版社</th> <th>出版日期</th> <th>价格</th> <th>创建时间</th> <th>更新时间</th> <th>操作</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.author }}</td> <td>{{ book.publisher }}</td> <td>{{ book.pub_date }}</td> <td>{{ book.price }}</td> <td>{{ book.create_time }}</td> <td>{{ book.update_time }}</td> <td> <a href="{% url 'book_detail' book.id %}">查看</a> <a href="{% url 'book_update' book.id %}">编辑</a> <a href="{% url 'book_delete' book.id %}">删除</a> </td> </tr> {% endfor %} </tbody> </table> <a href="{% url 'book_create' %}">添加图书</a> </body> </html> ``` `book_detail.html`: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书信息管理系统</title> </head> <body> <h1>图书信息详情</h1> <ul> <li>ID:{{ book.id }}</li> <li>标题:{{ book.title }}</li> <li>作者:{{ book.author }}</li> <li>出版社:{{ book.publisher }}</li> <li>出版日期:{{ book.pub_date }}</li> <li>价格:{{ book.price }}</li> <li>创建时间:{{ book.create_time }}</li> <li>更新时间:{{ book.update_time }}</li> </ul> <a href="{% url 'book_list' %}">返回列表</a> </body> </html> ``` `book_create.html`: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书信息管理系统</title> </head> <body> <h1>添加图书</h1> <form method="post"> {% csrf_token %} <label>标题:<input type="text" name="title"></label><br> <label>作者:<input type="text" name="author"></label><br> <label>出版社:<input type="text" name="publisher"></label><br> <label>出版日期:<input type="date" name="pub_date"></label><br> <label>价格:<input type="text" name="price"></label><br> <button type="submit">添加</button> </form> <a href="{% url 'book_list' %}">返回列表</a> </body> </html> ``` `book_update.html`: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书信息管理系统</title> </head> <body> <h1>编辑图书</h1> <form method="post"> {% csrf_token %} <label>标题:<input type="text" name="title" value="{{ book.title }}"></label><br> <label>作者:<input type="text" name="author" value="{{ book.author }}"></label><br> <label>出版社:<input type="text" name="publisher" value="{{ book.publisher }}"></label><br> <label>出版日期:<input type="date" name="pub_date" value="{{ book.pub_date }}"></label><br> <label>价格:<input type="text" name="price" value="{{ book.price }}"></label><br> <button type="submit">保存</button> </form> <a href="{% url 'book_list' %}">返回列表</a> </body> </html> ``` 7. 配置 URL 路由 在 `book/urls.py` 文件中添加以下内容: ```python from django.urls import path from . import views urlpatterns = [ path('', views.book_list, name='book_list'), path('<int:book_id>/', views.book_detail, name='book_detail'), path('create/', views.book_create, name='book_create'), path('<int:book_id>/update/', views.book_update, name='book_update'), path('<int:book_id>/delete/', views.book_delete, name='book_delete'), ] ``` 其中,`book_list` 对应图书信息列表页,`book_detail` 对应图书信息详情页,`book_create` 对应添加图书信息页,`book_update` 对应编辑图书信息页,`book_delete` 对应删除图书信息的操作。 8. 启动 Django 项目 ```bash python manage.py runserver ``` 打开浏览器访问 [http://127.0.0.1:8000/book/](http://127.0.0.1:8000/book/) 即可访问图书信息管理系统。 希望以上代码能对你有所帮助!

相关推荐

最新推荐

recommend-type

关于__Federico Milano 的电力系统分析工具箱.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

mlab-upenn 研究小组的心脏模型模拟.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

混合图像创建大师matlab代码.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

中序遍历二叉树-java版本

在Java中,实现二叉树的中序遍历同样可以通过递归来完成。中序遍历的顺序是:首先递归地中序遍历左子树,然后访问根节点,最后递归地中序遍历右子树。 在这段代码中,Node类定义了二叉树的节点,BinaryTree类包含一个指向根节点的指针和inOrder方法,用于递归地进行中序遍历。printInOrder方法调用inOrder方法并打印出遍历的结果。 在Main类中,我们创建了一个示例二叉树,并调用printInOrder方法来输出中序遍历的结果。输出应该是:4 2 5 1 3,这表示中序遍历的顺序是左子树(4),然后是根节点(2),接着是右子树的左子树(5),然后是右子树的根节点(1),最后是右子树的右子树(3)。
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLO目标检测算法的挑战与机遇:数据质量、计算资源与算法优化,探索未来发展方向

![:YOLO目标检测算法的挑战与机遇:数据质量、计算资源与算法优化,探索未来发展方向](https://img-blog.csdnimg.cn/7e3d12895feb4651b9748135c91e0f1a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rKJ6YaJ77yM5LqO6aOO5Lit,size_20,color_FFFFFF,t_70,g_se,x_16) # 1. YOLO目标检测算法简介 YOLO(You Only Look Once)是一种
recommend-type

def ObjFun(x,y,beta): # 目标函数 """ Logistic regression loss function :param beta: model parameter vector :param x: feature matrix :param y: label vector :return: loss value """ n = x.shape[0] p = x.shape[1] pred = 1 / (1 + np.exp(-np.dot(x, beta))) pred = np.clip(pred, 1e-15, 1 - 1e-15) # 将预测值限制在一个很小的区间内 ObjVal = -np.sum(y * np.log(pred) + (1 - y) * np.log(1 - pred)) / n return ObjVal

好的,根据上面的目标函数,我们可以推导出其对应的梯度向量函数,如下所示: def gradient(x, y, beta): """ Compute gradient of the logistic regression loss function :param beta: model parameter vector :param x: feature matrix :param y: label vector :return: gradient vector """ n = x.shape[0] pred = 1 /
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依