@[toc]
前言
上次写了用MySQL完成数据存储的图书管理系统,
这次我将把各个模块划分为系统的各个功能,通过flask框架与前端进行数据交互。做成一个小型的完整网页项目。
总的项目布局
项目布局很重要,什么类型的文件就放在对应类型的文件夹下。
blueprints :里放各个功能模块。
static :里面放css用的图片等美化网页的文件。(这个项目只是练手,前端就没细搞,所以我这个文件夹里是空的)
templates:里面放前端代码。
app.py:作为一个主文件运行,用来启动网站。
config.py:其实app.py下还应有个config.py作为配置文件。设置这样一个专门放项目配置的配置文件,就不用在每个项目中写几十甚至上百条配置语句了,配置时只需调用此文件即可。(这个项目很小,几乎用不到配置,所以没弄这个)
数据库层面:
这些是项目中用来存储数据的表。
效果展示
1.未登录时
有个提示登陆的弹窗,点确定就进入了
没有账号的需要注册
注册完成后就可以登录了
2.登录
若用普通学号(2028124001)登录,则功能较少
PS:项目较小,普通人登录的功能没怎么弄,就只能登录、注销和查看图书信息,更多功能都在管理员登录那。
注销,换个账号登录
就又回到了刚刚没登陆前的页面
这次用管理员学号(2028124078)登录
3.功能操作
管理员可以操作三个表:图书列表、已借出的书、借阅人信息,并可以对这三个表的数据进行“增删查”。(暂时没有“改”,等会了Ajax再加上)
具体操作就不一一展示了。
全部代码
话不多说,上代码!!!
blueprints里:
AllBooks.py
import pymysql
from flask import Blueprint, render_template, request, redirect, url_for
bp = Blueprint('AllBooks', __name__, url_prefix='/user')
@bp.route('/AllBooks', methods=['GET', 'POST'])
def AllBooks():
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'select * from allbooks'
try:
cursor.execute(sql)
f = cursor.fetchall()
context = {
'books': f,
}
except:
db.rollback()
db.close()
return render_template('AllBooks.html', **context)
@bp.route('/delete', methods=['GET', 'POST'])
def delete():
uid = request.args.get('uid')
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'delete from allbooks where 编号 = "%d"'
try:
cursor.execute(sql % int(uid)) # 要转换成int型,否则没功能
db.commit()
except:
db.rollback()
db.close()
return redirect(url_for('AllBooks.AllBooks'))
@bp.route('/search', methods=['GET', 'POST'])
def search():
data = request.args.get('search')
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'select * from allbooks where 编号 = "%d"'
try:
cursor.execute(sql % int(data))
f = cursor.fetchall()
context = {
'books': f
}
except:
db.rollback()
db.close()
return render_template('AllBooks.html', **context)
@bp.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'GET':
return render_template('addall.html')
else:
data = request.form
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'insert into allbooks(书名,作者,种类,数量,剩余) values("%s", "%s", "%s", "%d", "%d")' # SQL 插入语句
try:
cursor.execute(sql % (data['bookname'], data['writer'], data['typ'], int(data['qu']), int(data['sur']))) # 执行sql语句
db.commit()
except:
db.rollback() # 发生错误时回滚
db.close() # 关闭数据库连接
return redirect(url_for('AllBooks.AllBooks'))
BooksOfLent
import pymysql
from flask import Blueprint, render_template, request, url_for, redirect
bp = Blueprint('BooksOfLent', __name__, url_prefix='/lent')
@bp.route('/books', methods=['GET', 'POST'])
def lent():
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'select * from books_of_lent'
try:
cursor.execute(sql)
f = cursor.fetchall()
context = {
'books': f,
}
except:
db.rollback()
db.close()
return render_template('Books Of Lent.html', **context)
@bp.route('/delete', methods=['GET', 'POST'])
def delete():
uid = request.args.get('uid')
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'delete from books_of_lent where 编号 = "%d"'
try:
cursor.execute(sql % int(uid)) # 要转换成int型,否则没功能
db.commit()
except:
db.rollback()
db.close()
return redirect(url_for('BooksOfLent.lent'))
@bp.route('/search', methods=['GET', 'POST'])
def search():
data = request.args.get('search')
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'select * from books_of_lent where 编号 = "%d"'
try:
cursor.execute(sql % int(data))
f = cursor.fetchall()
context = {
'books': f
}
except:
db.rollback()
db.close()
return render_template('Books Of Lent.html', **context)
@bp.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'GET':
return render_template('addlent.html')
else:
data = request.form
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'insert into books_of_lent(编号,书名,借阅人学号,借出时间,归还时间) values("%d","%s", "%s", "%s", "%s")'
try:
cursor.execute(sql % (int(data['num']), data['bookname'], data['stunum'], data['lenttime'], (data['returntime']))) # 执行sql语句
db.commit()
except:
db.rollback() # 发生错误时回滚
db.close() # 关闭数据库连接
return redirect(url_for('BooksOfLent.lent'))
borrower
import pymysql
from flask import Blueprint, render_template, request
bp = Blueprint('borrower', __name__, url_prefix='/user')
@bp.route('/borrower')
def AllBooks():
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'select * from allbooks'
try:
cursor.execute(sql)
f = cursor.fetchall()
context = {
'books': f,
}
except:
db.rollback()
db.close()
return render_template('borrower.html', **context)
BorrowerInformation
import pymysql
from flask import Blueprint, render_template, request, url_for, redirect
bp = Blueprint('BorrowerInformation', __name__, url_prefix='/Borrower')
@bp.route('/Information', methods=['GET', 'POST'])
def borrower():
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'select * from borrower_information'
try:
cursor.execute(sql)
f = cursor.fetchall()
context = {
'inf': f
}
except:
db.rollback()
db.close()
return render_template('Borrower Information.html', **context)
@bp.route('/delete', methods=['GET', 'POST'])
def delete():
uid = request.args.get('uid')
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'delete from borrower_information where 学号 = "%s"'
try:
cursor.execute(sql % str(uid))
db.commit()
except:
db.rollback()
db.close()
return redirect(url_for('BorrowerInformation.borrower'))
@bp.route('/search', methods=['GET', 'POST'])
def search():
data = request.args.get('search')
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'select * from borrower_information where 学号 = "%s"'
# try:
cursor.execute(sql % data)
f = cursor.fetchall()
context = {
'inf': f
}
# except:
# db.rollback()
db.close()
return render_template('Borrower Information.html', **context)
@bp.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'GET':
return render_template('addinf.html')
else:
data = request.form
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'insert into borrower_information(学号,姓名) values("%s", "%s")' # SQL 插入语句
try:
cursor.execute(sql % (data['num'], data['name'])) # 执行sql语句
db.commit()
except:
db.rollback() # 发生错误时回滚
db.close() # 关闭数据库连接
return redirect(url_for('BorrowerInformation.borrower'))
register
import pymysql
from flask import Blueprint, render_template, request, redirect, url_for
bp = Blueprint('register', __name__, url_prefix='/register')
@bp.route('/people', methods=['GET', 'POST'])
def register():
if request.method == 'GET':
return render_template('register.html')
else:
data = request.form
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment') # 打开数据库连接
cursor = db.cursor() # 使用cursor()方法获取操作游标
sql = 'insert into borrower_information(学号,姓名) values("%s", "%s")' # SQL 插入语句
try:
cursor.execute(sql % (data['password'], data['username'])) # 执行sql语句
db.commit()
except:
db.rollback() # 发生错误时回滚
db.close() # 关闭数据库连接
return redirect(url_for('user.index'))
user
# home page and login
import pymysql
from flask import Blueprint, render_template, request, redirect, url_for
bp = Blueprint('user', __name__, url_prefix='/')
@bp.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'select * from allbooks'
try:
cursor.execute(sql)
f = cursor.fetchall()
context = {
'books': f,
}
except:
db.rollback()
db.close()
return render_template('AllBooks(Before login).html', **context)
else:
data = request.form
db = pymysql.connect(host='localhost', user="root", passwd='MQ20201008', database='final_assignment')
cursor = db.cursor()
sql = 'select * from borrower_information where 学号 = "%s"'
try:
cursor.execute(sql % data['password'])
f = cursor.fetchall()
if f:
return redirect(url_for('borrower.AllBooks'))
except:
db.rollback()
sql = 'select * from admin where 学号 = "%s"'
try:
cursor.execute(sql % data['password'])
f = cursor.fetchall()
if f:
return redirect(url_for('AllBooks.AllBooks'))
except:
db.rollback()
db.close()
templates里:
addall.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<style>
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body{
background: url("../demo_html/file/17.jpg" ) no-repeat ;
background-size: 1920px 1200px;
}
.rg_layout{
width: 800px;
height: 500px;
border: 5px solid #EEEEEE;
background-color: #FBFBEF;
margin-left: 100px;
margin-top: 180px;
}
.rg_left{
/*border: 1px red solid;*/
float: left;
margin: 15px;
}
.rg_center{
/*border: 1px red solid;*/
float: left;
margin-left: 60px;
margin-top: 40px;
}
.rg_right{
/*border: 1px red solid;*/
float: right;
margin: 10px;
}
.rg_left > p:first-child{
color: #FFD026;
font-size: 18px;
}
.rg_left >p:last-child{
color: #A6A6A6;
font-size: 16px;
}
.rg_right >p:first-child{
font-size: 10px;
}
.rg_right p a {
color: pink;
}
.td_left{
width: 100px;
height: 40px;
text-align: right;
}
.td_right{
padding-left: 20px;
}
#username,#password,#email,#name,#phone,#abirthday,#checkcod {
width: 180px;
height: 25px;
border: 1px solid #A6A6A6 ;
border-radius: 5px;
padding-left: 10px;
}
#checkcod{
width: 90px;
}
#img_check{
vertical-align: middle;
}
#btn_sub{
width: 100px;
height: 25px;
background-color: #FFD026;
border: solid #FFD026 1px;
margin-left: 40px;
margin-top: 20px;
}
</style>
</head>
<body>
<!-- 最外边的-->
<div class="rg_layout">
<!-- 最左边的-->
<div class="rg_left">
<p>新增</p>
<p>ADD</p>
</div>
<!--中间的-->
<div class="rg_center">
<div class="rg_from">
<form method="post" >
<table >
<tr>
<td class="td_left"><label>书名</label> </td>
<td class="td_right"> <input type="text" name="bookname" id="bookname" placeholder="请输入书名"> </td>
</tr>
<tr>
<td class="td_left"> <label>作者</label> </td>
<td class="td_right"> <input type="text" name="writer" id="writer" placeholder="请输入作者"> </td>
</tr>
<tr>
<td class="td_left"> <label>种类</label> </td>
<td class="td_right"> <input type="text" name="typ" id="typ" placeholder="请输入种类"> </td>
</tr>
<tr>
<td class="td_left"> <label>数量</label> </td>
<td class="td_right"> <input type="text" name="qu" id="qu" placeholder="请输入数量"> </td>
</tr>
<tr>
<td class="td_left"> <label>剩余</label> </td>
<td class="td_right"> <input type="text" name="sur" id="sur" placeholder="请输入剩余"> </td>
</tr>
<tr>
<td colspan="2" align="center"> <input type="submit" id="btn_sub" value="新增" > </td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
addinf.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<style>
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body{
background: url("../demo_html/file/17.jpg" ) no-repeat ;
background-size: 1920px 1200px;
}
.rg_layout{
width: 800px;
height: 500px;
border: 5px solid #EEEEEE;
background-color: #FBFBEF;
margin-left: 100px;
margin-top: 180px;
}
.rg_left{
/*border: 1px red solid;*/
float: left;
margin: 15px;
}
.rg_center{
/*border: 1px red solid;*/
float: left;
margin-left: 60px;
margin-top: 40px;
}
.rg_right{
/*border: 1px red solid;*/
float: right;
margin: 10px;
}
.rg_left > p:first-child{
color: #FFD026;
font-size: 18px;
}
.rg_left >p:last-child{
color: #A6A6A6;
font-size: 16px;
}
.rg_right >p:first-child{
font-size: 10px;
}
.rg_right p a {
color: pink;
}
.td_left{
width: 100px;
height: 40px;
text-align: right;
}
.td_right{
padding-left: 20px;
}
#username,#password,#email,#name,#phone,#abirthday,#checkcod {
width: 180px;
height: 25px;
border: 1px solid #A6A6A6 ;
border-radius: 5px;
padding-left: 10px;
}
#checkcod{
width: 90px;
}
#img_check{
vertical-align: middle;
}
#btn_sub{
width: 100px;
height: 25px;
background-color: #FFD026;
border: solid #FFD026 1px;
margin-left: 40px;
margin-top: 20px;
}
</style>
</head>
<body>
<!-- 最外边的-->
<div class="rg_layout">
<!-- 最左边的-->
<div class="rg_left">
<p>新增</p>
<p>ADD</p>
</div>
<!--中间的-->
<div class="rg_center">
<div class="rg_from">
<form method="post" >
<table >
<tr>
<td class="td_left"><label>学号</label> </td>
<td class="td_right"> <input type="text" name="num" id="num" placeholder="请输入学号"> </td>
</tr>
<tr>
<td class="td_left"> <label>姓名</label> </td>
<td class="td_right"> <input type="text" name="name" id="name" placeholder="请输入姓名"> </td>
</tr>
<tr>
<td colspan="2" align="center"> <input type="submit" id="btn_sub" value="新增" > </td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
addlent.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<style>
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body{
background: url("../demo_html/file/17.jpg" ) no-repeat ;
background-size: 1920px 1200px;
}
.rg_layout{
width: 800px;
height: 500px;
border: 5px solid #EEEEEE;
background-color: #FBFBEF;
margin-left: 100px;
margin-top: 180px;
}
.rg_left{
/*border: 1px red solid;*/
float: left;
margin: 15px;
}
.rg_center{
/*border: 1px red solid;*/
float: left;
margin-left: 60px;
margin-top: 40px;
}
.rg_right{
/*border: 1px red solid;*/
float: right;
margin: 10px;
}
.rg_left > p:first-child{
color: #FFD026;
font-size: 18px;
}
.rg_left >p:last-child{
color: #A6A6A6;
font-size: 16px;
}
.rg_right >p:first-child{
font-size: 10px;
}
.rg_right p a {
color: pink;
}
.td_left{
width: 100px;
height: 40px;
text-align: right;
}
.td_right{
padding-left: 20px;
}
#username,#password,#email,#name,#phone,#abirthday,#checkcod {
width: 180px;
height: 25px;
border: 1px solid #A6A6A6 ;
border-radius: 5px;
padding-left: 10px;
}
#checkcod{
width: 90px;
}
#img_check{
vertical-align: middle;
}
#btn_sub{
width: 100px;
height: 25px;
background-color: #FFD026;
border: solid #FFD026 1px;
margin-left: 40px;
margin-top: 20px;
}
</style>
</head>
<body>
<!-- 最外边的-->
<div class="rg_layout">
<!-- 最左边的-->
<div class="rg_left">
<p>新增</p>
<p>ADD</p>
</div>
<!--中间的-->
<div class="rg_center">
<div class="rg_from">
<form method="post" >
<table >
<tr>
<td class="td_left"><label>编号</label> </td>
<td class="td_right"> <input type="text" name="num" id="num" placeholder="请输入编号"> </td>
</tr>
<tr>
<td class="td_left"><label>书名</label> </td>
<td class="td_right"> <input type="text" name="bookname" id="bookname" placeholder="请输入书名"> </td>
</tr>
<tr>
<td class="td_left"> <label>学号</label> </td>
<td class="td_right"> <input type="text" name="stunum" id="stunum" placeholder="请输入借阅人学号"> </td>
</tr>
<tr>
<td class="td_left"> <label>借出时间</label> </td>
<td class="td_right"> <input type="text" name="lenttime" id="lenttime" placeholder="请输入借时 XXXX-XX-XX"> </td>
</tr>
<tr>
<td class="td_left"> <label>归还时间</label> </td>
<td class="td_right"> <input type="text" name="returntime" id="returntime" placeholder="请输入还时 XXXX-XX-XX"> </td>
</tr>
<tr>
<td colspan="2" align="center"> <input type="submit" id="btn_sub" value="新增" > </td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
AllBooks(Before login).html
<!--一打开就是这个页面,注册完跳回这个页面,普通用户登录也跳转回这个页面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书列表(登录前)</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script>alert("请先登录")</script>
</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="#">资料室图书管理系统</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="{{ url_for('register.register') }}"> 注册 <span class="sr-only">(current)</span></a></li>
</ul>
<form class="navbar-form navbar-left" method="post">
<div class="form-group">
<input type="text" class="form-control" placeholder="学号" name="password">
</div>
<button type="submit" class="btn btn-default">登录</button>
</form>
<ul class="nav navbar-nav navbar-right">
<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="#">注销</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="list-group">
<a href="#" class="list-group-item active">
图书列表
</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-primary">
<div class="panel-heading clearfix">
<h3 class="panel-title">图书馆管理系统<span class="glyphicon glyphicon-leaf pull-right"></span></h3>
</div>
<div class="panel-body">
<form class="form-inline pull-right">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" id="exampleInputAmount" placeholder="编号">
</div>
</div>
<button type="submit" class="btn btn-primary">搜索</button>
</form>
<table class="table table-hover table-striped table-bordered" style="margin-top: 10px">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>种类</th>
<th>数量</th>
<th>剩余</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ book[0] }}</td>
<td>{{ book[1] }}</td>
<td>{{ book[2] }}</td>
<td>{{ book[3] }}</td>
<td>{{ book[4] }}</td>
<td>{{ book[5] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
AllBooks.html
<!--图书列表(管理员)-->
<!--管理员登陆后跳到此页面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书列表(管理员)</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</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="#">资料室图书管理系统</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="{{ url_for('register.register') }}"> 注册 <span class="sr-only">(current)</span></a></li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="学号">
</div>
<button type="submit" class="btn btn-default">登录</button>
</form>
<ul class="nav navbar-nav navbar-right">
<!-- <li><a href="#">godlover</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="{{ url_for('user.index') }}">注销</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="list-group">
<a href="#" class="list-group-item active">
图书列表
</a>
<a href="{{url_for('BooksOfLent.lent')}}" class="list-group-item">已借出的书</a>
<a href="{{url_for('BorrowerInformation.borrower')}}" class="list-group-item">借阅人信息</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-primary">
<div class="panel-heading clearfix">
<h3 class="panel-title">图书馆管理系统<span class="glyphicon glyphicon-leaf pull-right"></span></h3>
</div>
<div class="panel-body">
<a href="{{ url_for('AllBooks.add') }}" class="btn btn-success">新增</a>
<form class="form-inline pull-right" method="GET" action="{{ url_for('AllBooks.search') }}">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="search" id="search" placeholder="编号">
</div>
</div>
<button type="submit" class="btn btn-primary">搜索</button>
</form>
<table class="table table-hover table-striped table-bordered" style="margin-top: 10px">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>种类</th>
<th>数量</th>
<th>剩余</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<form method="POST">
<tr>
<td>{{ book[0] }}</td>
<td>{{ book[1] }}</td>
<td>{{ book[2] }}</td>
<td>{{ book[3] }}</td>
<td>{{ book[4] }}</td>
<td>{{ book[5] }}</td>
<td class="text-center">
{# <a class="btn btn-primary btn-sm">编辑</a>#}
<a class="btn btn-danger btn-sm" href="{{ url_for('AllBooks.delete',uid=book[0]) }}">删除</a>
</td>
</tr>
</form>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Books Of Lent.html
<!--已借出的书-->
<!--管理员登录才能查看-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>已借出的书</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</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="#">资料室图书管理系统</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="{{ url_for('register.register') }}"> 注册 <span class="sr-only">(current)</span></a></li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="学号">
</div>
<button type="submit" class="btn btn-default">登录</button>
</form>
<ul class="nav navbar-nav navbar-right">
<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="{{ url_for('user.index') }}">注销</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="list-group">
<a href="{{ url_for('AllBooks.AllBooks') }}" class="list-group-item">图书列表</a>
<a href="#" class="list-group-item active">
已借出的书
</a>
<a href="{{url_for('BorrowerInformation.borrower')}}" class="list-group-item">借阅人信息</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-primary">
<div class="panel-heading clearfix">
<h3 class="panel-title">图书馆管理系统<span class="glyphicon glyphicon-leaf pull-right"></span></h3>
</div>
<div class="panel-body">
<a href="{{ url_for('BooksOfLent.add') }}" class="btn btn-success">新增</a>
<form class="form-inline pull-right" method="GET" action="{{ url_for('BooksOfLent.search') }}">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="search" id="search" placeholder="编号">
</div>
</div>
<button type="submit" class="btn btn-primary">搜索</button>
</form>
<table class="table table-hover table-striped table-bordered" style="margin-top: 10px">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>借阅人学号</th>
<th>借出时间</th>
<th>归还时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<form method="POST">
<tr>
<td>{{ book[0] }}</td>
<td>{{ book[1] }}</td>
<td>{{ book[2] }}</td>
<td>{{ book[3] }}</td>
<td>{{ book[4] }}</td>
<td class="text-center">
<a class="btn btn-danger btn-sm" href="{{ url_for('BooksOfLent.delete',uid=book[0]) }}">删除</a>
</td>
</tr>
</form>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
borrower.html
<!--一打开就是这个页面,注册完跳回这个页面,普通用户登录也跳转回这个页面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书列表(普通用户)</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<!--图书馆管理系统页面-->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<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="#">资料室图书管理系统</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="{{ url_for('register.register') }}"> 注册 <span class="sr-only">(current)</span></a></li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="学号">
</div>
<button type="submit" class="btn btn-default">登录</button>
</form>
<ul class="nav navbar-nav navbar-right">
<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="{{ url_for('user.index') }}">注销</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="list-group">
<a href="#" class="list-group-item active">
图书列表
</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-primary">
<div class="panel-heading clearfix">
<h3 class="panel-title">图书馆管理系统<span class="glyphicon glyphicon-leaf pull-right"></span></h3>
</div>
<div class="panel-body">
<table class="table table-hover table-striped table-bordered" style="margin-top: 10px">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>种类</th>
<th>数量</th>
<th>剩余</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ book[0] }}</td>
<td>{{ book[1] }}</td>
<td>{{ book[2] }}</td>
<td>{{ book[3] }}</td>
<td>{{ book[4] }}</td>
<td>{{ book[5] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Borrower Information.html
<!--借阅人信息-->
<!--管理员登录才能查看-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>借阅人信息</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<!--图书馆管理系统页面-->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<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="#">资料室图书管理系统</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="{{ url_for('register.register') }}"> 注册 <span class="sr-only">(current)</span></a></li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="学号">
</div>
<button type="submit" class="btn btn-default">登录</button>
</form>
<ul class="nav navbar-nav navbar-right">
<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="{{ url_for('user.index') }}">注销</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="list-group">
<a href="{{ url_for('AllBooks.AllBooks') }}" class="list-group-item">图书列表</a>
<a href="{{url_for('BooksOfLent.lent')}}" class="list-group-item">已借出的书</a>
<a href="#" class="list-group-item active">
借阅人信息
</a>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-primary">
<div class="panel-heading clearfix">
<h3 class="panel-title">图书馆管理系统<span class="glyphicon glyphicon-leaf pull-right"></span></h3>
</div>
<div class="panel-body">
<a href="{{ url_for('BorrowerInformation.add') }}" class="btn btn-success">新增</a>
<form class="form-inline pull-right" method="GET" action="{{ url_for('BorrowerInformation.search') }}">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="search" id="search" placeholder="学号">
</div>
</div>
<button type="submit" class="btn btn-primary">搜索</button>
</form>
<table class="table table-hover table-striped table-bordered" style="margin-top: 10px">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
{% for i in inf %}
<tr>
<td>{{ i[0] }}</td>
<td>{{ i[1] }}</td>
<td class="text-center">
<a class="btn btn-danger btn-sm" href="{{ url_for('BorrowerInformation.delete',uid=i[0]) }}">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<style>
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body{
background: url("../demo_html/file/17.jpg" ) no-repeat ;
background-size: 1920px 1200px;
}
.rg_layout{
width: 800px;
height: 500px;
border: 5px solid #EEEEEE;
background-color: #FBFBEF;
margin-left: 100px;
margin-top: 180px;
}
.rg_left{
/*border: 1px red solid;*/
float: left;
margin: 15px;
}
.rg_center{
/*border: 1px red solid;*/
float: left;
margin-left: 60px;
margin-top: 40px;
}
.rg_right{
/*border: 1px red solid;*/
float: right;
margin: 10px;
}
.rg_left > p:first-child{
color: #FFD026;
font-size: 18px;
}
.rg_left >p:last-child{
color: #A6A6A6;
font-size: 16px;
}
.rg_right >p:first-child{
font-size: 10px;
}
.rg_right p a {
color: pink;
}
.td_left{
width: 100px;
height: 40px;
text-align: right;
}
.td_right{
padding-left: 20px;
}
#username,#password,#email,#name,#phone,#abirthday,#checkcod {
width: 180px;
height: 25px;
border: 1px solid #A6A6A6 ;
border-radius: 5px;
padding-left: 10px;
}
#checkcod{
width: 90px;
}
#img_check{
vertical-align: middle;
}
#btn_sub{
width: 100px;
height: 25px;
background-color: #FFD026;
border: solid #FFD026 1px;
margin-left: 40px;
margin-top: 20px;
}
</style>
</head>
<body>
<!-- 最外边的-->
<div class="rg_layout">
<!-- 最左边的-->
<div class="rg_left">
<p >新用户注册</p>
<p>USER REGISIER</p>
</div>
<!--中间的-->
<div class="rg_center">
<div class="rg_from">
<form method="post">
<table >
<tr>
<td class="td_left"> <label>学号</label> </td>
<td class="td_right"> <input type="password" name="password" id="password" placeholder="请输入学号"> </td>
</tr>
<tr>
<td class="td_left"><label>姓名</label> </td>
<td class="td_right"> <input type="text" name="username" id="username" placeholder="请输入姓名"> </td>
</tr>
<tr>
<td colspan="2" align="center"> <input type="submit" id="btn_sub" value="注册" > </td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
其实该用模板继承呢,会大大减少重复代码量。我弄的这太冗余了!
app.py
from flask import Flask
from blueprints.user import bp as user_bp
from blueprints.register import bp as register_bp
from blueprints.AllBooks import bp as AllBooks_bp
from blueprints.BooksOfLent import bp as BooksOfLent_bp
from blueprints.BorrowerInformation import bp as BorrowerInformation_bp
from blueprints.borrower import bp as borrower_bp
app = Flask(__name__)
app.register_blueprint(user_bp)
app.register_blueprint(register_bp)
app.register_blueprint(AllBooks_bp)
app.register_blueprint(BooksOfLent_bp)
app.register_blueprint(BorrowerInformation_bp)
app.register_blueprint(borrower_bp)
if __name__ == '__main__':
app.run()
总结
这是我学了一个月时间做出来的小项目,功能很少,长得很丑,算是个半成品,跟各位大佬的肯定比不了,大家也就将就着看看,未来要是有时间还是会翻出来优化它的。
我之前学的很浅,只会用语言完成学校布置的一些简单题目而已,并不会用语言做其他东西。
机缘巧合之下,铁甲小宝大佬带我走进编程的世界,引领我学习。没有他,我现在还是个天天傻玩的憨批。
这个项目是我的第一个编程项目,算是正式入门了,今后我会更加努力学习的!!!
最后,再次感谢铁甲小宝大佬,祝大佬如愿以偿进大厂!!!