請你幫我檢查爲什麽資料無法寫入進去from flask import Flask, render_template, request import pandas as pd from openpyxl import load_workbook app = Flask(__name__) @app.route('/') def index(): return render_template('index2.html') @app.route('/submit', methods=['POST']) def submit(): # 获取表单数据 line = request.form['line'] date = request.form['date'] model = request.form['model'] lists = request.form['lists'] number = request.form['number'] prod_date = request.form['prod_date'] shift = request.form['shift'] prod_line = request.form['prod_line'] responsible = request.form['responsible'] # 表单验证:检查是否所有表单字段都已填写 if not all([line, date, model, lists, number, prod_date, shift, prod_line, responsible]): return "<script>alert('请填写完整表单信息!');history.back();</script>" else: # 在这里添加提交表单信息的代码 return "<script>alert('提交成功!');window.location.href='/';</script>" # 将数据存储到 Excel 文件中 file_path = 'D:/data.xlsx' wb = load_workbook(file_path) ws = wb.active ws.append([line, date, model, lists, number, prod_date, shift, prod_line, responsible]) wb.save(file_path) return render_template('index2.html')
时间: 2024-03-27 09:36:25 浏览: 129
基于Flask和Shell的pear_admin管理系统设计源码
可能是因为将数据存储到 Excel 文件中的代码放错位置了,导致数据无法写入。建议将以下几行代码移动到表单验证的 else 语句块内:
```python
# 将数据存储到 Excel 文件中
file_path = 'D:/data.xlsx'
wb = load_workbook(file_path)
ws = wb.active
ws.append([line, date, model, lists, number, prod_date, shift, prod_line, responsible])
wb.save(file_path)
```
这样就可以在表单验证通过后,将数据存储到 Excel 文件中了。
阅读全文