写一个基于flask的仓库管理系统程序
时间: 2024-06-10 12:03:55 浏览: 23
好的,我可以为您提供一个简单的仓库管理系统程序,基于 Flask 框架和 SQLite 数据库。
首先,您需要安装 Flask 和 SQLite。您可以通过 pip 安装它们,命令如下:
```
pip install flask
pip install sqlite3
```
然后,您需要创建一个名为 app.py 的 Python 文件,将以下代码复制并粘贴到文件中:
```python
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
# 配置数据库
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS products
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, quantity INTEGER NOT NULL)''')
conn.commit()
conn.close()
@app.route('/')
def index():
# 获取所有物品
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute("SELECT * FROM products")
products = c.fetchall()
conn.close()
return render_template('index.html', products=products)
@app.route('/add', methods=['GET', 'POST'])
def add_product():
if request.method == 'POST':
# 获取表单数据
name = request.form.get('name')
quantity = request.form.get('quantity')
# 存储到数据库
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute("INSERT INTO products (name, quantity) VALUES (?, ?)", (name, quantity))
conn.commit()
conn.close()
return redirect(url_for('index'))
else:
return render_template('add.html')
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit_product(id):
if request.method == 'POST':
# 获取表单数据
name = request.form.get('name')
quantity = request.form.get('quantity')
# 更新数据
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute("UPDATE products SET name=?, quantity=? WHERE id=?", (name, quantity, id))
conn.commit()
conn.close()
return redirect(url_for('index'))
else:
# 获取物品以填充表单
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute("SELECT * FROM products WHERE id=?", (id,))
product = c.fetchone()
conn.close()
return render_template('edit.html', product=product)
@app.route('/delete/<int:id>', methods=['POST'])
def delete_product(id):
# 删除物品
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute("DELETE FROM products WHERE id=?", (id,))
conn.commit()
conn.close()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()
```
接下来,您需要创建模板文件。创建一个名为 templates 的文件夹,并在其中创建以下文件:index.html、add.html 和 edit.html。将以下代码分别复制并粘贴到每个文件中:
index.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Inventory Management System</title>
</head>
<body>
<h1>Inventory</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product[1] }}</td>
<td>{{ product[2] }}</td>
<td>
<a href="{{ url_for('edit_product', id=product[0]) }}">Edit</a>
<form action="{{ url_for('delete_product', id=product[0]) }}" method="post">
<input type="submit" value="Delete">
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br/>
<a href="{{ url_for('add_product') }}">Add new product</a>
</body>
</html>
```
add.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Add Product</title>
</head>
<body>
<h1>Add Product</h1>
<form action="{{ url_for('add_product') }}" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity"><br>
<input type="submit" value="Submit">
</form>
<br/>
<a href="{{ url_for('index') }}">Back to inventory</a>
</body>
</html>
```
edit.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Edit Product</title>
</head>
<body>
<h1>Edit Product</h1>
<form action="{{ url_for('edit_product', id=product[0]) }}" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="{{ product[1] }}"><br>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" value="{{ product[2] }}"><br>
<input type="submit" value="Update">
</form>
<br/>
<a href="{{ url_for('index') }}">Back to inventory</a>
</body>
</html>
```
最后,运行 app.py 文件并在浏览器中访问 http://localhost:5000/ 以查看您的仓库管理系统。您应该能够添加、编辑和删除物品,并查看所有物品的列表。