用python flask框架写一个学生管理系统
时间: 2023-08-06 11:24:35 浏览: 115
好的,下面是一个使用 Flask 框架编写的学生管理系统的 Python 代码演示:
```python
from flask import Flask, render_template, request
app = Flask(__name__)
students = []
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
@app.route('/')
def index():
return render_template('index.html')
@app.route('/add_student', methods=['POST'])
def add_student():
name = request.form['name']
age = request.form['age']
gender = request.form['gender']
grade = request.form['grade']
student = Student(name, age, gender, grade)
students.append(student)
return render_template('add_student.html', name=name)
@app.route('/search_student', methods=['GET', 'POST'])
def search_student():
if request.method == 'POST':
name = request.form['name']
for student in students:
if student.name == name:
return render_template('search_student.html', student=student)
return render_template('search_student.html', error='未找到该学生!')
else:
return render_template('search_student.html')
@app.route('/remove_student', methods=['GET', 'POST'])
def remove_student():
if request.method == 'POST':
name = request.form['name']
for student in students:
if student.name == name:
students.remove(student)
return render_template('remove_student.html', name=name)
return render_template('remove_student.html', error='未找到该学生!')
else:
return render_template('remove_student.html')
@app.route('/show_all_students')
def show_all_students():
return render_template('show_all_students.html', students=students)
if __name__ == '__main__':
app.run()
```
这个学生管理系统使用了 Flask 框架,包含了添加学生、查找学生、删除学生、显示所有学生等功能,并使用了模板引擎来渲染页面。你可以根据自己的需求对这个代码进行修改和完善。注意在运行之前,需要在终端中使用 pip 安装 Flask 模块。
阅读全文