Python Flask - 上传 csv 文件并将其发布到数据库时需要插入时间戳
我正在创建一个表单,允许您上传 .csv 文件,然后在提交时将上传的文件添加到名为“文件”的表中的数据库中。
我想要弄清楚的是如何在上传文件时插入时间戳,以便在 HTML 端显示上传的文件以及上传/发布的日期和时间。
我之前尝试过的是给我当地时间,它会随着刷新而不断变化,但我想要文件上传的确切日期和时间。本质上,类似于您在文件资源管理器中单击文件属性时的“创建”时间戳。
我意识到我可能需要在我的 Files 类/表中为 DateTime 添加另一个属性,以便可以将其发布到数据库,然后呈现到 HTML 上。
这是我的“文件”类:
class Files(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
data = db.Column(db.LargeBinary)
f_source_id = db.Column(db.Integer, db.ForeignKey('funding_source.id'), nullable=True)
这是我将上传的文件发布到数据库的地方:
@main.route("/upload_file", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST' and 'csvfile' in request.files:
f = request.files['csvfile']
# pressing submit without uploading a file
if f.filename == "":
flash("No selected file. Please attach CSV file", "danger")
return redirect(url_for('main.alloc_summ'))
# only allowing .csv extension files
if f.filename and not allowed_file(f.filename):
flash("Only CSV Files Allowed", "danger")
return redirect(url_for('main.alloc_summ'))
# opening file, reading it in, and inserting values to database
if f.filename != '' and allowed_file(f.filename):
with open('<hidden path url>', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
new_file = Files(name=f.filename, data=f.read())
db.session.add(new_file)
db.session.commit()
flash("File Uploaded", "success")
return redirect(url_for('main.alloc_summ'))
如果有人可以指导我如何获取文件上传/发布到数据库时的时间戳,我将非常感激我可以查询文件表以在 HTML 端显示文件和时间戳。蒂亚!!
I'm creating a form that allows you to upload a .csv file and then on submit, adds the uploaded file to the database in a table called "Files".
What i'm trying to figure out is how to insert a timestamp for when the file is uploaded, so that on the HTML side, it shows the uploaded file, along with the date and time it was uploaded/posted.
What I tried before was giving me the local time, and it would keep changing with refresh, but I want the exact date and time the file is uploaded. In essence, something like the "Created" timestamp when you click on a file's properties in File Explorer.
I realize I may need to add another attribute for DateTime in my Files class/table so that it can be posted to the database, and then rendered onto the HTML.
Here is my class "Files":
class Files(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
data = db.Column(db.LargeBinary)
f_source_id = db.Column(db.Integer, db.ForeignKey('funding_source.id'), nullable=True)
Here is where i'm posting the uploaded file to the database:
@main.route("/upload_file", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST' and 'csvfile' in request.files:
f = request.files['csvfile']
# pressing submit without uploading a file
if f.filename == "":
flash("No selected file. Please attach CSV file", "danger")
return redirect(url_for('main.alloc_summ'))
# only allowing .csv extension files
if f.filename and not allowed_file(f.filename):
flash("Only CSV Files Allowed", "danger")
return redirect(url_for('main.alloc_summ'))
# opening file, reading it in, and inserting values to database
if f.filename != '' and allowed_file(f.filename):
with open('<hidden path url>', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
new_file = Files(name=f.filename, data=f.read())
db.session.add(new_file)
db.session.commit()
flash("File Uploaded", "success")
return redirect(url_for('main.alloc_summ'))
I would really appreciate it if anyone could guide me on how to get the timestamp for when a file is uploaded/posted to the database so that I can query the Files table to show the file and timestamp on the HTML side. TIA!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为如果您向数据库模型添加另一列来保存时间戳就足够了。创建数据库条目后,系统会自动填充一个值。所以你的要求应该得到满足。
您可以使用 Flask-Moment 显示时间戳。它使用 Moment.js 来显示。
I think it should be sufficient if you add another column to the database model that holds a timestamp. This is automatically filled with a value as soon as the database entry is created. So your requirements should be met.
You can use Flask-Moment to show the timestamp. It uses Moment.js to display.