python+flask实现简单的web端学生管理系统
发现之前写的代码丢了不少,好像自己什么都没学过,最近准备整理一下tensorflow的笔记,如果有什么问题或者错误,欢迎给我留言。
- 下面是代码,配置好flask环境,把html文件放到templates文件夹下,把python文件和templates文件夹放在同一目录下就可以运行了(python3.6)。
from flask import Flask,request,url_for,render_template,redirect
app=Flask(__name__)
student=[["张之林",'008',"物理院"],
["李林",'005',"数学院"],
["刘想",'002',"计科院"],
["高风",'003',"文学院"],
[" 杨桃 ",'001',"生科院"]]
def findnumber(Num): #这里定义一个查找学号的函数,方便复用
for i in range(len(student)):
if student[i][1]==Num:
return i
return -1
def save(stu): #定义一个保存的函数,以文本的形式把信息保存
studentfile=open('student.txt','w')
for i in stu:
for j in i:
str=j
studentfile.write(str)
studentfile.write(' '*4)
studentfile.write('\n')
studentfile.close()
@app.route('/') #这里是根目录,也就是打开链接第一个访问的页面
def index():
save(student)
return render_template('index.html',student=student) #这里的student=student,等号左边是要传给html文件的变量,右边是上面定义的二维列表
@app.route('/delstu',methods=['GET','POST']) #这里是删除模块,对应的页面是/delstu
def delstu():
if request.method=='POST':
delnumber = request.form.get('delnumber')
if findnumber(delnumber)==-1:
return '您输入的学号不存在'
else:
del student[findnumber(delnumber)]
save(student)
return redirect(url_for('index')) #如果找到了输入的学号执行删除操作,并重定向到首页
return render_template('delstu.html',student=student)
@app.route('/addstu',methods=['GET','POST']) #这里是添加模块
def addstu():
if request.method=='POST':
addnumber = request.form.get('addnumber')
if findnumber(addnumber)!=-1:
return "您输入的学号已存在"
else:
addname = request.form.get('addname')
adddepartment = request.form.get('adddepartment')
if addname != None and adddepartment and addnumber != None: #不为空则添加
student.append([addname, addnumber, adddepartment])
save(student)
return redirect(url_for('index'))
return render_template('addstu.html') #添加成功则重定向到首页
@app.route('/altstu',methods=['GET','POST']) #这里是修改模块,先要找到要修改的学号,如果找到了进行操作
def altstu():
if request.method=='POST':
altnumber = request.form.get('altnumber')
altdep = request.form.get('altdepartment')
altname = request.form.get('altname')
if findnumber(altnumber)==-1:
return '您输入的学号不存在'
else:
if altdep !='': #这里的一对单引号里什么都没有,表示如果输入框里为空,不进行修改
student[findnumber(altdep)][2]=altdep
if altname!='':
student[findnumber(altname)][0]=altname
save(student)
return render_template('index.html', student=student)
return render_template('altstu.html')
@app.route('/searchstu',methods=['GET','POST'])
def searchstu():
if request.method=='POST':
number = request.form.get('number')
if findnumber(number)==-1:
return '没有您要找的学号'
else:
find=student[findnumber(number)]
return render_template('searchstu.html',find=find) #如果找到了学号,把该学生信息传给html
return render_template('searchstu.html',student=student)
if __name__ == '__main__':
app.run(debug=True)
- 下面是html文件,需要把它们放到templates目录下才能进行渲染。
下面这个文件需要命名为addstu.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加</title>
<style type="text/css">
tbody{
font-size:25px;
color:grey;
background:EBD3E8;
height:50px;
width:500px;
}
</style>
</head>
<body bgcolor="FFDCB9">
<form action="{{url_for('addstu')}}" method="post">
<table >
<tbody>
<tr>
<td>学号:</td>
<td><input type="text" placehoder="请输入学号" name="addnumber">
</td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" placehoder="请输入姓名" name="addname">
</td>
</tr>
<tr>
<td>院系:</td>
<td><input type="text" placehoder="请输入院系" name="adddepartment">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="确定"></td>
</tr>
</tbody>
</table>
</form>
<br/>
<a href="{{url_for('index')}}">跳转到首页</a>
</body>
</html>
下面这个文件需要命名为altstu.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改</title>
<style type="text/css">
tbody{
font-size:25px;
color:grey;
background:EBD3E8;
height:50px;
width:500px;
}
</style>
</head>
<body bgcolor="FFDCB9">
<form action="{{url_for('altstu')}}" method="post">
<table>
<tbody>
<tr>
<td>请输入要修改的学号:</td>
<td><input type="text" placehoder="请输入学号" name="altnumber">
</td>
</tr>
<tr>
<td>请输入要修改的姓名:</td>
<td><input type="text" placehoder="请输入姓名" name="altname">
</td>
</tr>
<tr>
<td>请输入要修改的院系:</td>
<td><input type="text" placehoder="请输入院系" name="altdepartment">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="确定"></td>
</tr>
</tbody>
</table>
</form>
<a href="{{url_for('index')}}">跳转到首页</a>
</body>
</html>
下面这个文件需要命名为delstu.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>删除</title>
<style type="text/css">
tbody{
font-size:25px;
color:grey;
background:EBD3E8;
height:50px;
width:500px;
}
</style>
</head>
<body bgcolor="FFDCB9">
<form action="{{url_for('delstu')}}" method="post">
<table>
<tbody>
<tr>
<td>请输入要删除的学号:</td>
<td><input type="text" placehoder="请输入要删除的学号" name="delnumber">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="确定"></td>
</tr>
</tbody>
</table>
</form>
<br/>
<a href="{{url_for('index')}}">跳转到首页</a>
</body>
</html>
下面这个文件需要命名为index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生管理系统</title>
<style type="text/css">
ul{
font-size:30px;
color:grey;
background:613030;
height:50px;
width:500px;
}
a{
font-size:25px;
color:purple;
background:FBD3E5;
height:50px;
width:500px;
}
</style>
</head>
<body bgcolor="FFDCB9">
<table border="2">
<tbody>
<tr>
<td><a href="{{url_for('delstu')}}">删除学生信息</a></td>
</tr>
<tr>
<td><a href="{{url_for('addstu')}}">增加学生信息</a></td>
</tr>
<tr>
<td><a href="{{url_for('altstu')}}">修改学生信息</a></td>
</tr>
<tr>
<td><a href="{{url_for('searchstu')}}">查找学生信息</a></td>
</tr>
</tbody>
</table>
</br>
<table border="1">
{% for i in student %}
<tr>
<td><ul><li>{{ i }}</li></ul></td>
</tr>
{% endfor %}
</table>
</body>
</html>
下面这个文件需要命名为searchstu.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查找</title>
<style type="text/css">
tbody{
font-size:25px;
color:grey;
background:EBD3E8;
height:50px;
width:500px;
}
</style>
</head>
<body bgcolor="FFDCB9">
<form action="{{url_for('searchstu')}}" method="post">
<table>
<tbody>
<tr>
<td>学号:</td>
<td><input type="text" placehoder="请输入学号" name="number">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="确定"></td>
</tr>
</tbody>
</table>
</form>
<br/>
{%if find%}
您查找的学生信息为:{{find}}
{%endif%}
<br/>
<a href="{{url_for('index')}}">跳转到首页</a>
</body>
</html>