flask 学生成绩管理系统
时间: 2023-11-17 22:04:47 浏览: 113
以下是一个基于Flask框架的学生成绩管理系统的简单实现,包括学生信息的增删改查和成绩的录入和查询功能:
```python
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
students = [
{'id': '001', 'name': '张三', 'score': {'语文': 80, '数学': 90, '英语': 85}},
{'id': '002', 'name': '李四', 'score': {'语文': 75, '数学': 95, '英语': 80}},
{'id': '003', 'name': '王五', 'score': {'语文': 85, '数学': 80, '英语': 90}}
]
# 学生信息管理
@app.route('/student')
def student():
return render_template('student.html', students=students)
# 添加学生信息
@app.route('/add_student', methods=['POST'])
def add_student():
id = request.form['id']
name = request.form['name']
score = {}
for key in request.form:
if key not in ['id', 'name']:
score[key] = int(request.form[key])
students.append({'id': id, 'name': name, 'score': score})
return redirect(url_for('student'))
# 删除学生信息
@app.route('/delete_student/<id>')
def delete_student(id):
for student in students:
if student['id'] == id:
students.remove(student)
break
return redirect(url_for('student'))
# 修改学生信息
@app.route('/edit_student/<id>')
def edit_student(id):
for student in students:
if student['id'] == id:
return render_template('edit_student.html', student=student)
return redirect(url_for('student'))
# 更新学生信息
@app.route('/update_student', methods=['POST'])
def update_student():
id = request.form['id']
name = request.form['name']
score = {}
for key in request.form:
if key not in ['id', 'name']:
score[key] = int(request.form[key])
for student in students:
if student['id'] == id:
student['name'] = name
student['score'] = score
break
return redirect(url_for('student'))
# 成绩录入
@app.route('/score_input/<id>')
def score_input(id):
for student in students:
if student['id'] == id:
return render_template('score_input.html', student=student)
return redirect(url_for('student'))
# 成绩查询
@app.route('/score_query/<id>')
def score_query(id):
for student in students:
if student['id'] == id:
return render_template('score_query.html', student=student)
return redirect(url_for('student'))
if __name__ == '__main__':
app.run(debug=True)
```
在上述代码中,我们使用了Flask框架来搭建学生成绩管理系统,其中包括了学生信息的增删改查和成绩的录入和查询功能。我们使用了Flask提供的`render_template`函数来渲染HTML模板,使用了`request`对象来获取表单数据,使用了`redirect`函数来重定向到指定的URL。
阅读全文