图书管理系统BMS
效果图:
主要代码:
表关系的创建:
from django.db import models
# Create your models here.
class Book(models.Model):
"""图书表"""
title = models.CharField(max_length=32, verbose_name='书名')
price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='价格')
publish_time = models.DateField(auto_now_add=True)
# kucun = models.IntegerField(verbose_name='库存数', default=1000) # 设置默认值
# maichu = models.IntegerField(verbose_name='卖出数', null=True) # 允许为空
# 书籍与出版社的一对多关系字段
publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
# 书籍与作者的多对多关系字段(自动创建第三张表)
authors = models.ManyToManyField(to='Author')
def __str__(self):
return f'书籍对象:{self.title}'
class Publish(models.Model):
"""出版社表"""
name = models.CharField(max_length=32, verbose_name='名称')
address = models.CharField(max_length=64, verbose_name='地址')
def __str__(self):
return f'出版社对象:{self.name}'
class Author(models.Model):
"""作者表"""
name = models.CharField(max_length=32, verbose_name='姓名')
age = models.IntegerField(verbose_name='年龄')
# 作者与作者详情的一对一关系
author_detail = models.OneToOneField(to='AuthorDetail', on_delete=models.CASCADE)
def __str__(self):
return f'作者对象:{self.name}'
class AuthorDetail(models.Model):
"""作者详情表"""
phone = models.BigIntegerField(verbose_name='电话号码')
address = models.CharField(max_length=32, verbose_name='家庭地址')
urls:
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
# 首页的编写
path('home/', views.home_func,name='home_view'),
# 图书列表展示页
path('/book_list',views.book_list_func,name='book_list_view'),
# 图书数据添加页
path('/book_add', views.book_add_func, name='book_add_view'),
# 图书数据编辑页
path('/book_edit/<int:book_pk>/', views.book_edit_func,name='book_edit_view'),
# 图书数据删除接口
path('book_delete/<int:book_pk>/', views.book_delete_func, name='book_delete_view')
]
views:
from django.shortcuts import render,redirect,HttpResponse
from app01 import models
# Create your views here.
def home_func(request):
return render(request,'homePage.html')
def book_list_func(request):
# 1.获取所有的数据 # queryset [数据对象,数据对象]
book_queryset = models.Book.objects.all()
# 2.返回html页面并在页面上展示图书数据
return render(request,'bookListPage.html',locals())
def book_add_func(request):
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
publish_time = request.POST.get('publish_time')
publish_id = request.POST.get('publish_id')
author_id_list = request.POST.get('author_id_list')
book_obj = models.Book.objects.create(title=title, price=price, publish_time=publish_time,
publish_id=publish_id
)
book_obj.authors.add(*author_id_list) # add(1,2,3)
# 重定向到书籍展示页
return redirect('book_list_view') # 只支持没有动态匹配的别名反向解析
# 2.分析可得页面需要出版社和作者的全部数据
publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
# 1.返回一个可以让用户输入书籍相关数据的html页面
return render(request,'bookAddPage.html',locals())
def book_edit_func(request, book_pk):
# 1.根据匹配到的数据主键值查询数据对象
edit_obj = models.Book.objects.filter(pk=book_pk).first()
if request.method == 'POST':
title = request.POST.get('title') # 列表最后一个数据值
price = request.POST.get('price') # 列表最后一个数据值
publish_time = request.POST.get('publish_time') # 列表最后一个数据值
publish_id = request.POST.get('publish_id') # 列表最后一个数据值
author_id_list = request.POST.getlist('author_id_list') # 获取完整的列表数据
models.Book.objects.filter(pk=book_pk).update(title=title, price=price, publish_time=publish_time,
publish_id=publish_id)
edit_obj.authors.set(author_id_list)
return redirect('book_list_view')
# 3.分析可得编辑页也需要所有的出版社和作者信息
publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
# 2.传递给html页面并展示给用户看
return render(request, 'bookEditPage.html', locals())
def book_delete_func(request, book_pk):
# 删除书籍与作者的绑定关系
delete_book_obj = models.Book.objects.filter(pk=book_pk).first()
delete_book_obj.authors.clear()
# 删除书籍数据
models.Book.objects.filter(pk=book_pk).delete()
return redirect('book_list_view')
homePage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap-3.4.1-dist/css/bootstrap.min.css' %}">
<script src="{% static 'bootstrap-3.4.1-dist/js/bootstrap.min.js' %}"></script>
{% block css %}
{% endblock %}
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">BMS</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">图书 <span class="sr-only">(current)</span></a></li>
<li><a href="#">作者</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">查询</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Jia</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多操作 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
<div class="list-group">
<a href="{% url 'home_view' %}" class="list-group-item active">首页展示</a>
<a href="{% url 'book_list_view' %}" class="list-group-item list-group-item-success">图书列表</a>
<a href="#" class="list-group-item list-group-item-info">出版社列表</a>
<a href="#" class="list-group-item list-group-item-warning">作者列表</a>
<a href="#" class="list-group-item list-group-item-danger">更多详情</a>
</div>
<div class="list-group">
<a href="{% url 'home_view' %}" class="list-group-item active">首页展示</a>
<a href="#" class="list-group-item list-group-item-success">图书列表</a>
<a href="#" class="list-group-item list-group-item-info">出版社列表</a>
<a href="#" class="list-group-item list-group-item-warning">作者列表</a>
<a href="#" class="list-group-item list-group-item-danger">更多详情</a>
</div>
<div class="list-group">
<a href="{% url 'home_view' %}" class="list-group-item active">首页展示</a>
<a href="#" class="list-group-item list-group-item-success">图书列表</a>
<a href="#" class="list-group-item list-group-item-info">出版社列表</a>
<a href="#" class="list-group-item list-group-item-warning">作者列表</a>
<a href="#" class="list-group-item list-group-item-danger">更多详情</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">BMS</h3>
</div>
{% block content %}
<div class="panel-body">
<div class="page-header">
<h1>图书管理系统<small>Subtext for header</small></h1>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img0.baidu.com/it/u=2261625525,2052751300&fm=253&fmt=auto&app=138&f=JPEG?w=490&h=500" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img1.baidu.com/it/u=3510388477,472874873&fm=253&fmt=auto&app=120&f=JPEG?w=350&h=350" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="http://t13.baidu.com/it/u=3150272070,2516947182&fm=224&app=112&f=JPEG?w=500&h=500" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img0.baidu.com/it/u=3978485405,2459936864&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500" alt="...">
</a>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img0.baidu.com/it/u=2261625525,2052751300&fm=253&fmt=auto&app=138&f=JPEG?w=490&h=500" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img1.baidu.com/it/u=3510388477,472874873&fm=253&fmt=auto&app=120&f=JPEG?w=350&h=350" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="http://t13.baidu.com/it/u=3150272070,2516947182&fm=224&app=112&f=JPEG?w=500&h=500" alt="...">
</a>
</div>
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="https://img0.baidu.com/it/u=3978485405,2459936864&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500" alt="...">
</a>
</div>
</div>
{% endblock %}
</div>
</div>
</div>
</div>
{% block js %}
{% endblock %}
</body>
</html>
bookListPage.html
{% extends 'homePage.html'%}
{% block content %}
<h2>图书展示页</h2>
<a href="{% url 'book_add_view' %}" class="btn btn-success">图书添加</a>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>价格</th>
<th>日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book_obj in book_queryset %}
<tr>
<td>{{ forloop.counter }}</td> <!--展示主键值也可以展示从1开始的有序数列-->
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.price }}</td>
<td>{{ book_obj.publish_time|date:'Y-m-d' }}</td>
<td>{{ book_obj.publish.name }}</td>
<td>
{% for author_obj in book_obj.authors.all %}
{% if forloop.last %}
{{ author_obj.name }}
{% else %}
{{ author_obj.name }},
{% endif %}
{% endfor %}
</td>
<td>
<a href="{% url 'book_edit_view' book_obj.pk %}" class="btn btn-primary btn-xs">编辑</a>
<a href="{% url 'book_delete_view' book_obj.pk %}" class="btn btn-danger btn-xs delBtn">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation" class="text-center">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true"><<</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">>></span>
</a>
</li>
</ul>
</nav>
{% endblock %}
{% block js %}
<script>
$('.delBtn').click(function () {
let isDel = confirm('你确定要删除吗?')
if(!isDel){
return false
}
})
</script>
{% endblock %}
bookEditPage.html:
{% extends 'homePage.html' %}
{% block content %}
<h2 class="text-center">书籍编辑页</h2>
<form action="" method="post">
<p>title:
<input type="text" name="title" class="form-control" value="{{ edit_obj.title }}">
</p>
<p>price:
<input type="text" name="price" class="form-control" value="{{ edit_obj.price }}">
</p>
<p>publish_time:
<input type="date" name="publish_time" class="form-control" value="{{ edit_obj.publish_time|date:'Y-m-d' }}">
</p>
<p>publish_name:
<!--展示系统中已有的出版社信息供选择-->
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_queryset %} <!--queryset [数据对象 数据对象]-->
<!--根据待编辑的书籍对象查询对应的出版社 与系统所有的出版社比对 如果相同则添加默认选中的属性 selected-->
{% if edit_obj.publish == publish_obj %}
<option value="{{ publish_obj.pk }}" selected>{{ publish_obj.name }}</option>
{% else %}
<option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<p>author_list:
<!--展示系统中已有的作者信息供选择-->
<select name="author_id_list" id="" multiple class="form-control">
{% for author_obj in author_queryset %} <!--queryset [数据对象 数据对象]-->
<!--成员运算判断循环的作者对应是否在待编辑的书籍作者对象列表中-->
{% if author_obj in edit_obj.authors.all %}
<option value="{{ author_obj.pk }}" selected>{{ author_obj.name }}</option>
{% else %}
<option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<input type="submit" value="书籍编辑" class="btn btn-block btn-danger">
</form>
{% endblock %}
bookAddPage.html
{% extends 'homePage.html' %}
{% block content %}
<h2 class="text-center">图书添加页</h2>
<form action="" method="post">
<p>
title:
<input type="text" name="title" class="form-control">
</p>
<p>
price:
<input type="text" name="price" class="form-control">
</p>
<p>
publish_name:
<!--展示系统中的出版社信息供选择-->
<select name="publish_id" id="" class="form-control">
{% for publish_obj in publish_queryset %}
<option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
{% endfor %}
</select>
</p>
<p>
author_list:
<select name="author_id_list" id="" multiple class="form-control">
{% for author_obj in author_queryset %}
<option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
{% endfor %}
</select>
</p>
<input type="submit" value="书籍添加" class="btn btn-block btn-success" >
</form>
{% endblock %}
标签:obj,name,管理系统,models,author,publish,BMS,book,图书
From: https://www.cnblogs.com/zx0524/p/17013465.html