我正在尝试导出一个CSV文件,生成该文件需要一段时间。我已经编写了导出CSV视图,该视图可以成功运行,但是我需要对其进行调整,使其能够使用Celepie,以便在生产环境中处理大量数据。我编写了以下代码(主要遵循https://thoslin.github.io/async-download-with-celery/):
查看:
from tasks import export_report
@staff_member_required()
def export_status_report(request):
task = export_report.delay()
return render(request, "admin/poll_for_download.html", {"task_id": task.task_id })
# Check if task is finished
def poll_for_download(request):
task_id = request.GET.get("task_id")
filename = request.GET.get("filename")
if request.is_ajax():
result = AsyncResult(task_id)
if result.ready():
return HttpResponse(json.dumps({"filename": result.get()}))
return HttpResponse(json.dumps({"filename": None}))
try:
f = open("/path/to/export/"+filename)
except:
return HttpResponseForbidden()
else:
response = HttpResponse(file, mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
任务:
@app.task
def export_report():
date = datetime.now()
date = date.strftime("%m/%d/%Y")
filename = "reoport"+date+".csv"
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename= "{}"'.format(filename)
payouts = Payout.objects.all()
writer = csv.writer(response) #instantiate writer
# write headers
writer.writerow(['Field1', 'Field2', 'Field3'])
for payout in payouts:
list = [payout.field1, payout.field2, payout.field3]
writer.writerow(list)
return filename
超文本标记语言
<script>
$(function(){
$.ajaxSetup({ cache: false, timeout: 360000 });
var url = "admin/poll-for-download/";
var i = 0;
(function worker() {
$.getJSON(url+"?task_id=", function(data){
if(data.filename) {
var file_url = url+"?filename="+data.filename;
$("#content").html("If your download doesn't start automatically, please click <a href='"+file_url+"'>here</a>.");
window.location.href = file_url;
} else {
setTimeout(worker, 5000);
}
});
})();
setInterval(function() {
i = ++i % 4;
$("#loading").html("loading"+Array(i+1).join("."));
}, 1000);
});
但是,这不起作用。我收到一个错误:
with open("%s%s" % ("/path/to/export/", filename), "w+") as f:
2022-11-10T14:03:49.564942+00:00 app[worker.1]: FileNotFoundError: [Errno 2] No such file or directory: '/path/to/export/31e39843-421d-49f5-8a59-46394c22a3ce.csv'
如何解决这个问题,以便脚本在准备就绪时自动下载CSV并向用户显示链接,以防下载无法自动启动。我也不希望报告公开,因此不应将其上传到我们的S3/Cloudfront静态文件夹。
1条答案
按热度按时间ruyhziif1#
替换
file
任务
response = HttpResponse(file, mimetype='text/csv')
与
response = HttpResponse(filename, mimetype='text/csv')
或f
文件对象