flask人事管理系统代码实现
时间: 2023-10-01 14:07:54 浏览: 60
下面是一个简单的 Flask 人事管理系统代码实现示例:
```
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
# 连接数据库
def get_db_connection():
conn = sqlite3.connect('employee.db')
conn.row_factory = sqlite3.Row
return conn
# 关闭数据库连接
def close_db_connection(conn):
conn.close()
# 显示所有员工信息
@app.route('/')
def show_employees():
conn = get_db_connection()
cur = conn.cursor()
cur.execute('SELECT * FROM employees')
employees = cur.fetchall()
close_db_connection(conn)
return render_template('employees.html', employees=employees)
# 添加员工
@app.route('/add_employee', methods=['GET', 'POST'])
def add_employee():
if request.method == 'POST':
name = request.form['name']
position = request.form['position']
conn = get_db_connection()
conn.execute('INSERT INTO employees (name, position) VALUES (?, ?)', (name, position))
conn.commit()
close_db_connection(conn)
return redirect(url_for('show_employees'))
return render_template('add_employee.html')
# 编辑员工
@app.route('/edit_employee/<int:id>', methods=['GET', 'POST'])
def edit_employee(id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute('SELECT * FROM employees WHERE id = ?', (id,))
employee = cur.fetchone()
if request.method == 'POST':
name = request.form['name']
position = request.form['position']
conn.execute('UPDATE employees SET name = ?, position = ? WHERE id = ?', (name, position, id))
conn.commit()
close_db_connection(conn)
return redirect(url_for('show_employees'))
close_db_connection(conn)
return render_template('edit_employee.html', employee=employee)
# 删除员工
@app.route('/delete_employee/<int:id>')
def delete_employee(id):
conn = get_db_connection()
conn.execute('DELETE FROM employees WHERE id = ?', (id,))
conn.commit()
close_db_connection(conn)
return redirect(url_for('show_employees'))
if __name__ == '__main__':
app.run(debug=True)
```
这个示例实现了一个简单的人事管理系统,包括显示员工信息、添加员工、编辑员工和删除员工等功能。其中使用了 SQLite 数据库来存储员工信息。你可以根据自己的需求进行修改和扩展。
阅读全文