如何利用python的flask写一个考勤管理的网页
时间: 2024-01-01 13:05:58 浏览: 117
下面是一个简单的考勤管理网页的示例,使用 Flask 和 SQLite 数据库进行开发:
首先,我们需要创建一个虚拟环境并安装 Flask 和 SQLite:
```
$ python -m venv venv
$ source venv/bin/activate
$ pip install flask
$ pip install sqlite3
```
然后,我们需要创建一个名为 `app.py` 的文件,并添加以下代码:
```python
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
# 配置数据库
conn = sqlite3.connect('attendance.db', check_same_thread=False)
c = conn.cursor()
# 创建表
c.execute('''CREATE TABLE IF NOT EXISTS attendance
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
status TEXT NOT NULL,
date TEXT NOT NULL)''')
conn.commit()
# 关闭数据库连接
c.close()
# 路由
@app.route('/')
def index():
# 获取所有的考勤记录
conn = sqlite3.connect('attendance.db', check_same_thread=False)
c = conn.cursor()
c.execute("SELECT * FROM attendance ORDER BY date DESC")
rows = c.fetchall()
c.close()
return render_template('index.html', rows=rows)
@app.route('/add', methods=['POST'])
def add():
# 添加新的考勤记录
name = request.form['name']
status = request.form['status']
date = request.form['date']
conn = sqlite3.connect('attendance.db', check_same_thread=False)
c = conn.cursor()
c.execute("INSERT INTO attendance (name, status, date) VALUES (?, ?, ?)",
(name, status, date))
conn.commit()
c.close()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
```
在上面的代码中,我们首先导入必要的库,然后配置 SQLite 数据库并创建一个名为 `attendance` 的表。接下来,我们定义两个路由:一个是主页,它将获取所有的考勤记录并将它们传递给一个名为 `index.html` 的模板进行渲染;另一个是添加新的考勤记录,它将从表单中获取考勤信息并将其添加到数据库中。
最后,我们使用 Flask 的 `run()` 方法启动应用程序并设置 `debug=True` 以便于调试。
接下来,我们需要创建一个名为 `index.html` 的模板,并添加以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Attendance Management</title>
</head>
<body>
<h1>Attendance Management</h1>
<table>
<tr>
<th>Name</th>
<th>Status</th>
<th>Date</th>
</tr>
{% for row in rows %}
<tr>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
</tr>
{% endfor %}
</table>
<form method="post" action="{{ url_for('add') }}">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required>
<br>
<label for="status">Status:</label>
<select name="status" id="status" required>
<option value="Present">Present</option>
<option value="Absent">Absent</option>
</select>
<br>
<label for="date">Date:</label>
<input type="date" name="date" id="date" required>
<br>
<input type="submit" value="Add">
</form>
</body>
</html>
```
在上面的代码中,我们首先定义一个表来显示考勤记录。然后,我们使用模板语言循环遍历所有的考勤记录,并将它们显示在表格中。最后,我们添加一个表单,用于添加新的考勤记录。
启动应用程序并在浏览器中访问 `http://localhost:5000/`,您将看到一个简单的考勤管理网页。您可以使用表单添加新的考勤记录,然后在主页上查看所有的考勤记录。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)