我有一个索引视图,显示培训师名称,位置等,以及下载他们的个人资料的按钮(已作为pdf在数据库中)。我根据一些参数搜索并过滤这些参数。现在我希望能够选择其中一些并下载所选的Trainer Profiles.Im是Django的新手,无法弄清楚如何做到这一点。这是我的代码。它说'FieldFile对象不可调用' views.py def下载(请求): 导入zipfile import os
file = zipfile.ZipFile("C:\\Downloads\\test.zip", "w")
filelist = []
filelist+= 'Trainer.checkboxoption' in request.REQUEST
for i in filelist:
file.write(i)
file.close()
return render(request,'trainer/index.html')
的index.html
<form action="download/" method="post">
<div class="caption">
<div >
<table style="width:100%" class="table">
<tr>
<th>#</th>
<th>Name</th>
<th>Technology</th>
<th>Location</th>
<th> View</th>
<th>Download</th>
<th>Delete</th>
</tr>
{% for trainer in all_trainers %}
<tr>
<td><input type="checkbox" id="trainer{{ forloop.counter }}" name="trainer" value="{{ trainer.id }}"></td>
<td> <a href="/trainer/{{ trainer.id }}">{{ trainer.name }}</td>
<td>{{ trainer.technology }}</td></a>
<td>{{ trainer.location }}</td>
<!-- View Details -->
<td><a href="/trainer/{{ trainer.id }}" class="btn btn-primary btn-sm">View Details</a></td>
<td><a href="../media/{{ trainer.trainer_profile }}" class="btn">Download PDF</a></td>
<!-- Delete Album -->
<td><form action="trainer/{{trainer.id }}/delete/" method="post">
{% csrf_token %}
<input type="hidden" name="trainer_id" value="{{ trainer.id }}" />
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form></td>
</tr>
{% endfor %}
</table>
</div>
</div>
<input type="submit" value="Download ZIP">
</form>
答案 0 :(得分:0)
views.py:
def download(request):
import zipfile
filef = zipfile.ZipFile("test.zip", "w")
var = request.POST.getlist('checks')
pdfprofile = []
for i in var:
trainer= Trainer.objects.get(pk=i)
pdfprofile.append(trainer.trainer_profile)
for i in pdfprofile:
filef.write("D:\\Django\\myproduct\\media\\"+str(i))
filef.close()
response = HttpResponse(open("test.zip", "rb").read(), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=test.zip'
return response