当我单击按钮下载文件时,似乎第一次工作,然后即使数据库已更新,也下载了该文件。第一次之后似乎没有通过出口定义。谁能告诉我为什么会这样吗?
这是按钮和路线的代码:
<a class="btn btn-success" href="{{ url_for('exports') }}" role="button">Export CSV</a>
@app.route('/exports', methods=['GET', 'POST'])
def exports():
outfile = open('exports/export.csv', 'w')
outcsv = csv.writer(outfile)
records = db.session.query(Choice).all()
[outcsv.writerow([getattr(curr, column.name) for column in Choice.__mapper__.columns]) for curr in records]
outfile.close()
return send_file('exports/export.csv', as_attachment=True)