请帮用flask和mysql生成一套学生管理系统的代码,实现系统登录,学生信息的增删改查功能。请生成python代码和html代码哦

时间: 2024-05-02 11:17:29 浏览: 12
好的,以下是使用 Flask 和 MySQL 实现学生管理系统的 Python 代码和 HTML 代码。 Python 代码: ```python from flask import Flask, render_template, request, redirect, url_for import pymysql app = Flask(__name__) app.config['SECRET_KEY'] = 'secret_key' # MySQL连接配置 conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='password', db='db_name', charset='utf8') # 登录页面 @app.route('/', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] cursor = conn.cursor() cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password)) user = cursor.fetchone() if user: return redirect(url_for('students')) else: return render_template('login.html', error='用户名或密码错误') else: return render_template('login.html') # 学生列表页面 @app.route('/students') def students(): cursor = conn.cursor() cursor.execute("SELECT * FROM students") students = cursor.fetchall() return render_template('students.html', students=students) # 添加学生页面 @app.route('/add_student', methods=['GET', 'POST']) def add_student(): if request.method == 'POST': name = request.form['name'] age = request.form['age'] sex = request.form['sex'] department = request.form['department'] cursor = conn.cursor() cursor.execute("INSERT INTO students(name, age, sex, department) VALUES(%s, %s, %s, %s)", (name, age, sex, department)) conn.commit() return redirect(url_for('students')) else: return render_template('add_student.html') # 修改学生页面 @app.route('/edit_student/<int:id>', methods=['GET', 'POST']) def edit_student(id): if request.method == 'POST': name = request.form['name'] age = request.form['age'] sex = request.form['sex'] department = request.form['department'] cursor = conn.cursor() cursor.execute("UPDATE students SET name=%s, age=%s, sex=%s, department=%s WHERE id=%s", (name, age, sex, department, id)) conn.commit() return redirect(url_for('students')) else: cursor = conn.cursor() cursor.execute("SELECT * FROM students WHERE id=%s", id) student = cursor.fetchone() return render_template('edit_student.html', student=student) # 删除学生 @app.route('/delete_student/<int:id>') def delete_student(id): cursor = conn.cursor() cursor.execute("DELETE FROM students WHERE id=%s", id) conn.commit() return redirect(url_for('students')) if __name__ == '__main__': app.run() ``` HTML 代码: `login.html` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生管理系统 - 登录</title> </head> <body> <h1>学生管理系统 - 登录</h1> {% if error %} <p style="color: red;">{{ error }}</p> {% endif %} <form method="post"> <label>用户名:</label> <input type="text" name="username" required><br><br> <label>密码:</label> <input type="password" name="password" required><br><br> <input type="submit" value="登录"> </form> </body> </html> ``` `students.html` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生管理系统 - 学生列表</title> </head> <body> <h1>学生管理系统 - 学生列表</h1> <a href="/add_student">添加学生</a><br><br> <table border="1"> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>系别</th> <th>操作</th> </tr> {% for student in students %} <tr> <td>{{ student[0] }}</td> <td>{{ student[1] }}</td> <td>{{ student[2] }}</td> <td>{{ student[3] }}</td> <td>{{ student[4] }}</td> <td> <a href="/edit_student/{{ student[0] }}">修改</a> <a href="/delete_student/{{ student[0] }}" onclick="return confirm('确定要删除吗?')">删除</a> </td> </tr> {% endfor %} </table> </body> </html> ``` `add_student.html` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生管理系统 - 添加学生</title> </head> <body> <h1>学生管理系统 - 添加学生</h1> <form method="post"> <label>姓名:</label> <input type="text" name="name" required><br><br> <label>年龄:</label> <input type="number" name="age" required><br><br> <label>性别:</label> <input type="radio" name="sex" value="男" checked>男 <input type="radio" name="sex" value="女">女<br><br> <label>系别:</label> <input type="text" name="department" required><br><br> <input type="submit" value="添加"> </form> <br> <a href="/students">返回学生列表</a> </body> </html> ``` `edit_student.html` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生管理系统 - 修改学生</title> </head> <body> <h1>学生管理系统 - 修改学生</h1> <form method="post"> <label>姓名:</label> <input type="text" name="name" value="{{ student[1] }}" required><br><br> <label>年龄:</label> <input type="number" name="age" value="{{ student[2] }}" required><br><br> <label>性别:</label> <input type="radio" name="sex" value="男" {% if student[3] == '男' %}checked{% endif %}>男 <input type="radio" name="sex" value="女" {% if student[3] == '女' %}checked{% endif %}>女<br><br> <label>系别:</label> <input type="text" name="department" value="{{ student[4] }}" required><br><br> <input type="submit" value="修改"> </form> <br> <a href="/students">返回学生列表</a> </body> </html> ``` 注意:需要根据实际情况修改 MySQL 连接配置和数据库表结构。

相关推荐

最新推荐

recommend-type

Python实现的查询mysql数据库并通过邮件发送信息功能

主要介绍了Python实现的查询mysql数据库并通过邮件发送信息功能,可实现Python针对mysql的查询与宕机信息通过邮件发送的相关操作技巧,需要的朋友可以参考下
recommend-type

Python Flask微信小程序登录流程及登录api实现代码

主要介绍了Python Flask微信小程序登录流程及登录api实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

python实现通过flask和前端进行数据收发

今天小编就为大家分享一篇python实现通过flask和前端进行数据收发,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python flask实现分页的示例代码

主要介绍了python flask实现分页的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

Flask实现普通用户和管理员用户同页面登录

Flask实现普通用户和管理员用户同页面登录 1.效果图 1.1前端登录页面 招聘企业 管理员
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。