?上次發(fā)的一篇文章,有很多朋友私信我要后面的部分,那咱們就今天來一起學習一下吧,因為我的數(shù)據(jù)庫這門課選中的課題是學生管理系統(tǒng),所以今天就以這個課題為例子,從0到1去實現(xiàn)一個管理系統(tǒng)。數(shù)據(jù)庫設(shè)計部分我會專門出一個博客的,敬請期待吧~~~
介如很多朋友問源碼,我已經(jīng)將它上傳到github上。(內(nèi)有sql文件,可直接導入數(shù)據(jù)庫使用)
看到這了點個贊再走吧
wuyongch/-Student-management-system: 該學生管理系統(tǒng)使用python+flask框架+mysql數(shù)據(jù)庫 實現(xiàn)學生錄入、學生信息修改、學生課程錄入和查詢、畢業(yè)學生去向查詢、教師開設(shè)課程查看、管理超級用戶 (github.com)
效果展現(xiàn)
一、實現(xiàn)登錄功能
這里我就不細講了,感興趣的可以看下面這個博客??
(5條消息) python Web開發(fā) flask輕量級Web框架實戰(zhàn)項目--實現(xiàn)功能--賬號密碼登錄界面(連接數(shù)據(jù)庫Mysql)_flask web開發(fā)實戰(zhàn)_吳永暢的博客-CSDN博客
這次有點不同的是 需要把登錄功能封裝成一個login函數(shù),然后我為了省事呢就把數(shù)據(jù)庫連接放在外部。
代碼實現(xiàn)(前端登錄界面在上方博客里)
# 初始化
app = flask.Flask(__name__)
# 使用pymysql.connect方法連接本地mysql數(shù)據(jù)庫
db = pymysql.connect(host='localhost', port=3306, user='root',
password='root', database='student', charset='utf8')
# 操作數(shù)據(jù)庫,獲取db下的cursor對象
cursor = db.cursor()
# 存儲登陸用戶的名字用戶其它網(wǎng)頁的顯示
users = []
@app.route("/", methods=["GET", "POST"])
def login():
# 增加會話保護機制(未登陸前l(fā)ogin的session值為空)
flask.session['login'] = ''
if flask.request.method == 'POST':
user = flask.request.values.get("user", "")
pwd = flask.request.values.get("pwd", "")
# 防止sql注入,如:select * from admins where admin_name = '' or 1=1 -- and password='';
# 利用正則表達式進行輸入判斷
result_user = re.search(r"^[a-zA-Z]+$", user) # 限制用戶名為全字母
result_pwd = re.search(r"^[a-zA-Z\d]+$", pwd) # 限制密碼為 字母和數(shù)字的組合
if result_user != None and result_pwd != None: # 驗證通過
msg = '用戶名或密碼錯誤'
# 正則驗證通過后與數(shù)據(jù)庫中數(shù)據(jù)進行比較
# sql = "select * from sys_user where username='" + \
# user + "' and password='" + pwd + "';"
sql1 = "select * from admins where admin_name='" + \
user + " ' and admin_password='" + pwd + "';"
# cursor.execute(sql)
cursor.execute(sql1)
result = cursor.fetchone()
# 匹配得到結(jié)果即管理員數(shù)據(jù)庫中存在此管理員
if result:
# 登陸成功
flask.session['login'] = 'OK'
users.append(user) # 存儲登陸成功的用戶名用于顯示
return flask.redirect(flask.url_for('student'))
# return flask.redirect('/file')
else: # 輸入驗證不通過
msg = '非法輸入'
else:
msg = ''
user = ''
return flask.render_template('login.html', msg=msg, user=user)
二、學生信息錄入功能
?這里我們可以錄入的信息是學生學號、學生姓名、班級、性別等。
首先用戶登錄成功之后,跳轉(zhuǎn)到學生信息錄入界面,系統(tǒng)需要顯示出學生表信息。
if flask.request.method == 'GET':
sql_list = "select * from students_infos"
cursor.execute(sql_list)
results = cursor.fetchall()
if flask.request.method == 'POST':
# 獲取輸入的學生信息
student_id = flask.request.values.get("student_id", "")
student_class = flask.request.values.get("student_class", "")
student_name = flask.request.values.get("student_name", "")
student_sex = flask.request.values.get("student_sex")
print(student_id, student_class, student_name, student_sex)
插入數(shù)據(jù)只需要寫入sql語句,并且執(zhí)行該語句就可以,異常處理是個人習慣,在插入失敗是系統(tǒng)給予提示,這里的sql語句都是寫活的,真正的數(shù)據(jù)是來源于頁面輸入,其實就是調(diào)用了sql語句插入成功后,學生表就會自動同步顯示在前端頁面上。
完整代碼如下
@app.route('/student', methods=['GET', "POST"])
def student():
# login session值
if flask.session.get("login", "") == '':
# 用戶沒有登陸
print('用戶還沒有登陸!即將重定向!')
return flask.redirect('/')
insert_result = ''
# 當用戶登陸有存儲信息時顯示用戶名,否則為空
if users:
for user in users:
user_info = user
else:
user_info = ''
# 獲取顯示數(shù)據(jù)信息
if flask.request.method == 'GET':
sql_list = "select * from students_infos"
cursor.execute(sql_list)
results = cursor.fetchall()
if flask.request.method == 'POST':
# 獲取輸入的學生信息
student_id = flask.request.values.get("student_id", "")
student_class = flask.request.values.get("student_class", "")
student_name = flask.request.values.get("student_name", "")
student_sex = flask.request.values.get("student_sex")
print(student_id, student_class, student_name, student_sex)
try:
# 信息存入數(shù)據(jù)庫
sql = "create table if not exists students_infos(student_id varchar(10) primary key,student_class varchar(100),student_name varchar(32),student_sex VARCHAR(4));"
cursor.execute(sql)
sql_1 = "insert into students_infos(student_id, student_class, student_name, student_sex )values(%s,%s,%s,%s)"
cursor.execute(sql_1, (student_id, student_class, student_name, student_sex))
insert_result = "成功存入一條學生信息"
print(insert_result)
except Exception as err:
print(err)
insert_result = "學生信息插入失敗"
print(insert_result)
pass
db.commit()
# POST方法時顯示數(shù)據(jù)
sql_list = "select * from students_infos"
cursor.execute(sql_list)
results = cursor.fetchall()
return flask.render_template('student.html', insert_result=insert_result, user_info=user_info, results=results)
student前端頁面(需要自取,點個贊哦)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>學生成績管理系統(tǒng)</title>
<link rel="icon" >
<link rel="stylesheet" type="text/css" href="../static/css/bootstrap.min.css"/>
<style>
.container {
position: absolute;
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
li {
list-style: none;
}
a {
text-decoration: none;
color: white;
}
.header {
position: relative;
width: 100%;
height: 55px;
background-color: black;
}
.left {
position: absolute;
left: 20px;
font-size: 20px;
line-height: 55px;
color: white;
text-align: center;
}
.right {
position: absolute;
right: 160px;
line-height: 55px;
color: white;
text-align: center;
}
.right_right {
position: absolute;
right: 24px;
line-height: 55px;
color: white;
text-align: center;
}
.leftside {
float: left;
background-color: rgb(245, 245, 245);
/* height: 663px; */
width: 230px;
}
.leftside ul {
height: 663px;
}
.leftside .first {
background-color: rgb(66, 139, 202);
margin-top: 25px;
}
.leftside .first a {
color: white;
}
.leftside {
float: left;
width: 200px;
height: 100%;
}
.leftside ul li {
border-bottom: 0.2px solid white;
font-size: 20px;
height: 60px;
line-height: 60px;
width: 100%;
text-align: center;
}
.container-fluid {
float: none;
width: 100%;
height: 100%;
}
/* .sub-header {
margin-top: 5px;
} */
.table-responsive {
margin-top: 10px;
}
.table-striped {
width: 1250px;
}
thead tr th {
background-color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<span class="left">學 生 信 息 錄 入</span>
<span class="right">你 好,{{user_info}}管 理 員!</span>
<span class="right_right"><a href="/">退出登陸</a></span>
</div>
<div class="leftside">
<ul>
<li class="first"><a href="/student">學生信息錄入</a></li>
<li><a href ="/updata_student">學生信息修改</a></li>
<li><a href="/teacher_class">老師開設(shè)課程查看</a></li>
<li><a href="/teacher">選課信息錄入</a></li>
<li><a href="/grade">成績信息錄入</a></li>
<li><a href="/grade_infos">學生成績查詢</a></li>
<li><a href="/graduation">畢業(yè)去向</a></li>
<li><a href="/adminstator">系統(tǒng)管理員變動</a></li>
</ul>
</div>
<div class="container-fluid">
<h1 class="sub-header">學 生 信 息 錄 入 系 統(tǒng)</h1>
<hr>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>學號</th>
<th>班級</th>
<th>姓名</th>
<th>性別</th>
</tr>
</thead>
<tbody>
<tr>
<form action="" method="post">
<td><input class="long" name="student_id" type="text" /> </td>
<td><input class="long" name="student_class" type="text" />
</td>
<td><input class="long" name="student_name"
type="text" />
</td>
<td><input class="long" name="student_sex"
type="text" />
</td>
<td><input class="last" type="submit" value="提交" /> </td>
<td><span>提交結(jié)果:{{insert_result}}</span></td>
</form>
</tr>
<tr>
<td>學生學號</td>
<td>所屬班級</td>
<td>學生姓名</td>
<td>學生性別</td>
</tr>
{% for result in results %}
<tr>
<td>{{result[0]}}</td>
<td>{{result[1]}}</td>
<td>{{result[2]}}</td>
<td>{{result[3]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
三、學生信息變動功能(修改班級和姓名or刪除學生)
如圖所示 吳永暢現(xiàn)在的班級是軟件工程212 現(xiàn)在要修改為軟件工程213 那么就在下拉框里選擇修改學生班級,然后把學生學號和姓名填入,在學生班級那里填入修改后的班級。
?修改后:
原理都是一樣的,執(zhí)行對應的sql語句即可,這里就不贅述了。?
?完整代碼
@app.route('/updata_student', methods=['GET', "POST"])
def updata_student():
# login session值
if flask.session.get("login", "") == '':
# 用戶沒有登陸
print('用戶還沒有登陸!即將重定向!')
return flask.redirect('/')
insert_result = ''
# 獲取顯示學生數(shù)據(jù)信息(GET方法的時候顯示數(shù)據(jù))
if flask.request.method == 'GET':
sql_list = "select * from students_infos"
cursor.execute(sql_list)
results = cursor.fetchall()
# 當用戶登陸有存儲信息時顯示用戶名,否則為空
if users:
for user in users:
user_info = user
else:
user_info = ''
if flask.request.method == 'POST':
# 獲取輸入的學生信息
student_id = flask.request.values.get("student_id", "")
student_class = flask.request.values.get("student_class", "")
student_name = flask.request.values.get("student_name", "")
# student_sex = flask.request.values.get("student_sex", "")
student_id_result = re.search(r"^\d{8,}$", student_id) # 限制用戶名為全字母
# 驗證通過
if student_id_result != None: # 驗證通過
# 獲取下拉框的數(shù)據(jù)
select = flask.request.form.get('selected_one')
if select == '修改學生班級':
try:
sql = "update students_infos set student_class=%s where student_id=%s;"
cursor.execute(sql, (student_class, student_id))
insert_result = "學生" + student_id + "的班級修改成功!"
except Exception as err:
print(err)
insert_result = "修改學生班級失敗!"
pass
db.commit()
if select == '修改學生姓名':
try:
sql = "update students_infos set student_name=%s where student_id=%s;"
cursor.execute(sql, (student_name, student_id))
insert_result = "學生" + student_name + "的姓名修改成功!"
except Exception as err:
print(err)
insert_result = "修改學生姓名失敗!"
pass
db.commit()
if select == '刪除學生':
try:
sql_delete = "delete from students_infos where student_id='" + student_id + "';"
cursor.execute(sql_delete)
insert_result = "成功刪除學生" + student_id
except Exception as err:
print(err)
insert_result = "刪除失敗"
pass
db.commit()
else: # 輸入驗證不通過
insert_result = "輸入的格式不符合要求!"
# POST方法時顯示數(shù)據(jù)
sql_list = "select * from students_infos"
cursor.execute(sql_list)
results = cursor.fetchall()
return flask.render_template('updata_student.html', user_info=user_info, insert_result=insert_result,
results=results)
前端頁面?updata_student.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>學生成績管理系統(tǒng)</title>
<link rel="icon" >
<link rel="stylesheet" type="text/css" href="../static/css/bootstrap.min.css"/>
<style>
.container {
position: absolute;
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
li {
list-style: none;
}
a {
text-decoration: none;
color: white;
}
.header {
position: relative;
width: 100%;
height: 55px;
background-color: black;
}
.left {
position: absolute;
left: 20px;
font-size: 20px;
line-height: 55px;
color: white;
text-align: center;
}
.right {
position: absolute;
right: 160px;
line-height: 55px;
color: white;
text-align: center;
}
.right_right {
position: absolute;
right: 24px;
line-height: 55px;
color: white;
text-align: center;
}
.leftside {
float: left;
background-color: rgb(245, 245, 245);
/* height: 663px; */
width: 230px;
}
.leftside ul {
height: 663px;
}
.leftside .first {
background-color: rgb(66, 139, 202);
margin-top: 25px;
}
.leftside .first a {
color: white;
}
.leftside {
float: left;
width: 200px;
height: 100%;
}
.leftside ul li {
border-bottom: 0.2px solid white;
font-size: 20px;
height: 60px;
line-height: 60px;
width: 100%;
text-align: center;
}
.container-fluid {
float: none;
width: 100%;
height: 100%;
}
.table-responsive {
margin-top: 10px;
}
.table-striped {
width: 1250px;
}
thead tr th {
background-color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<span class="left">學 生 信 息 修 改</span>
<span class="right">你 好,{{user_info}}管 理 員!</span>
<span class="right_right"><a href="/">退出登陸</a></span>
</div>
<div class="leftside">
<ul>
<li><a href="/student">學生信息錄入</a></li>
<li class="first"><a href="#">學生信息變動</a></li>
<li><a href="/teacher_class">老師開設(shè)課程查看</a></li>
<li><a href="/teacher">選課信息錄入</a></li>
<li><a href="/grade">成績信息錄入</a></li>
<li><a href="/grade_infos">學生成績查詢</a></li>
<li><a href="/graduation">畢業(yè)去向</a></li>
<li><a href="/adminstator">系統(tǒng)管理員變動</a></li>
</ul>
</div>
<div class="container-fluid">
<h1 class="sub-header">學 生 信 息 修 改 系 統(tǒng)</h1>
<hr>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>學生學號<span>(限全數(shù)字的學號)</span></th>
<th>學生班級<span></span></th>
<th>學生姓名<span></span></th>
<th>學生性別<span></span></th>-->
<th>選擇(修改/刪除)學生</th>
</tr>
</thead>
<tbody>
<tr>
<form action="" method="post">
<td><input class="long" name="student_id"
type="text" />
</td>
<td><input class="long" name="student_class" type="text" /> </td>
<td><input class="long" name="student_name"
type="text" />
</td>
<td><select name="selected_one">
<option value="修改學生班級">修改學生班級</option>
<option value="修改學生姓名">修改學生姓名</option>
<option value="刪除學生 ">刪除學生</option>
</select></td>
<td><input class="last" type="submit" value="操作" /> </td>
<td class="doit"><span>操作結(jié)果:{{insert_result}}</span></td>
</form>
</tr>
<tr>
<td>學號</td>
<td>班級</td>
<td>姓名</td>
</tr>
{% for result in results %}
<tr>
<td>{{result[0]}}</td>
<td>{{result[1]}}</td>
<td>{{result[2]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
四、學生成績查詢功能
如下圖查詢學號為20212248的學生成績
?這里我們設(shè)計幾種常見的查詢方式,大家也可以自己添加。
?
完整代碼?
@app.route('/grade_infos', methods=['GET', 'POST'])
def grade_infos():
# login session值
if flask.session.get("login", "") == '':
# 用戶沒有登陸
print('用戶還沒有登陸!即將重定向!')
return flask.redirect('/')
query_result = ''
results = ''
# 當用戶登陸有存儲信息時顯示用戶名,否則為空
if users:
for user in users:
user_info = user
else:
user_info = ''
# 獲取下拉框的數(shù)據(jù)
if flask.request.method == 'POST':
select = flask.request.form.get('selected_one')
query = flask.request.values.get('query')
print(select, query)
# 判斷不同輸入對數(shù)據(jù)表進行不同的處理
if select == '學號':
try:
sql = "select * from grade_infos where student_id = %s; "
cursor.execute(sql, query)
results = cursor.fetchall()
if results:
query_result = '查詢成功!'
else:
query_result = '查詢失敗!'
except Exception as err:
print(err)
pass
if select == '姓名':
try:
sql = "select * from grade_infos where student_id in(select student_id from students_infos where student_name=%s);"
cursor.execute(sql, query)
results = cursor.fetchall()
if results:
query_result = '查詢成功!'
else:
query_result = '查詢失敗!'
except Exception as err:
print(err)
pass
if select == '課程名稱':
try:
sql = "select * from grade_infos where student_class_id in(select student_class_id from students_infos where student_class_id=%s);"
cursor.execute(sql, query)
results = cursor.fetchall()
if results:
query_result = '查詢成功!'
else:
query_result = '查詢失敗!'
except Exception as err:
print(err)
pass
if select == "所在班級":
try:
sql = "select * from grade_infos where student_class_id in(select student_class_id from students_infos where student_class=%s);"
cursor.execute(sql, query)
results = cursor.fetchall()
if results:
query_result = '查詢成功!'
else:
query_result = '查詢失敗!'
except Exception as err:
print(err)
pass
return flask.render_template('grade_infos.html', query_result=query_result, user_info=user_info, results=results)
前端頁面?grade_infos.html文章來源:http://www.zghlxwxcb.cn/news/detail-559469.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>學生成績管理系統(tǒng)</title>
<link rel="icon" >
<link rel="stylesheet" type="text/css" href="../static/css/bootstrap.min.css"/>
<style>
.container {
position: absolute;
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
li {
list-style: none;
}
a {
text-decoration: none;
color: white;
}
.header {
position: relative;
width: 100%;
height: 55px;
background-color: black;
}
.left {
position: absolute;
left: 20px;
font-size: 20px;
line-height: 55px;
color: white;
text-align: center;
}
.right {
position: absolute;
right: 160px;
line-height: 55px;
color: white;
text-align: center;
}
.right_right {
position: absolute;
right: 24px;
line-height: 55px;
color: white;
text-align: center;
}
.leftside {
float: left;
background-color: rgb(245, 245, 245);
/* height: 663px; */
width: 230px;
}
.leftside ul {
height: 663px;
}
.leftside .first {
background-color: rgb(66, 139, 202);
margin-top: 25px;
}
.leftside .first a {
color: white;
}
.leftside {
float: left;
width: 200px;
height: 100%;
}
.leftside ul li {
border-bottom: 0.2px solid white;
font-size: 20px;
height: 60px;
line-height: 60px;
width: 100%;
text-align: center;
}
.container-fluid {
float: none;
width: 100%;
height: 100%;
}
.table-responsive {
margin-top: 10px;
}
.table-striped {
width: 1250px;
}
thead tr th {
background-color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<span class="left">學 生 成 績 查 詢</span>
<span class="right">你 好,{{user_info}}老 師!</span>
<span class="right_right"><a href="/">退出登陸</a></span>
</div>
<div class="leftside">
<ul>
<li><a href="/student">學生信息錄入</a></li>
<li><a href ="/updata_student">學生信息修改</a></li>
<li><a href="/teacher_class">老師開設(shè)課程查看</a></li>
<li><a href="/teacher">選課信息錄入</a></li>
<li><a href="/grade">成績信息錄入</a></li>
<li class="first"><a href="#">學生成績查詢</a></li>
<li><a href="/graduation">畢業(yè)去向</a></li>
<li><a href="/adminstator">系統(tǒng)管理員變動</a></li>
</ul>
</div>
<div class="container-fluid">
<h1 class="sub-header">學 生 成 績 查 詢 系 統(tǒng)</h1>
<hr>
<div class="table-responsive">
<table class="table table-striped" cellspaing="10">
<tbody>
<tr>
<form action="" method="post">
<td><label for="#">請選擇查詢的方式:(學號/姓名/課程名稱/所在班級)</label> </td>
<td><select name="selected_one">
<option value="學號" selected="selected">學號</option>
<option value="姓名">姓名</option>
<option value="課程號">課程號</option>
<option value="所在班級">所在班級</option>
</select></td>
<td><input class="long" type="text" name="query"></td>
<td><input class="last" type="submit" value="查詢" /></td>
<td><span>查詢結(jié)果:{{query_result}}</span></td>
</form>
</tr>
<tr>
<td>學生學號</td>
<td>課程號</td>
<td>成績</td>
</tr>
{% for result in results %}
<tr>
<td>{{result[0]}}</td>
<td>{{result[1]}}</td>
<td>{{result[2]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
?五、總結(jié)
因為實現(xiàn)原理都是相似的,所以我就不一個個去寫了,就把增刪查改功能各寫一遍,剩下感興趣的可以自己去嘗試寫下,要相信自己!總體來說還是一個值得初學者去練習的項目。文章來源地址http://www.zghlxwxcb.cn/news/detail-559469.html
到了這里,關(guān)于python Web開發(fā) flask輕量級Web框架實戰(zhàn)項目--學生管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!