Я пытаюсь получить несколько экземпляров из набора запросов, затем для каждого экземпляра создать PDF-файл из объекта HttpResponse, используя пакет xhtml2pdf , а затем в конечном итоге сжать все сгенерированные PDFS для загрузки.
Это то, что я имею до сих пор:
def bulk_cover_letter(request, ward_id, school_cat_id):
report_files = {}
school_type = SchoolType.objects.get(id=school_cat_id)
ward = Ward.objects.get(id=ward_id)
schools_in_school_type = Applicant.objects.filter(
school_type=school_type, ward_id=ward_id, award_status='awarded').order_by().values_list(
'school_name', flat=True).distinct()
for school in schools_in_school_type:
cheque_number = Applicant.objects.filter(school_type=school_type, ward_id=ward_id, award_status='awarded', school_name=school).values_list('cheque_number__cheque_number', flat=True).distinct()
beneficiaries = Applicant.objects.filter(school_type=school_type, ward_id=ward_id, award_status='awarded', school_name=school)
total_amount_to_beneficiaries = Applicant.objects.filter(school_type=school_type, ward_id=ward_id, award_status='awarded', school_name=school).aggregate(total=Sum('school_type__amount_allocated'))
context = {
'school_name' : school,
'beneficiaries' : beneficiaries,
'total_amount_to_beneficiaries' : total_amount_to_beneficiaries,
'title' : school + ' Disbursement Details',
'cheque_number': cheque_number[0]
}
response = HttpResponse('<title>Cover Letter</title>', content_type='application/pdf')
filename = "%s.pdf" %(cheque_number[0])
content = "inline; filename=%s" %(filename)
response['Content-Disposition'] = content
template = get_template('cover_letter.html')
html = template.render(context)
mem_fp = io.BytesIO()
pdf = pisa.CreatePDF(
html, dest=mem_fp, link_callback=link_callback)
resp = HttpResponse(mem_fp.getvalue(), content_type='application/pdf')
resp['Content-Disposition'] = "attachment; filename=%s" %(filename)
report_files[filename] = resp
mem_zip = io.BytesIO()
with zipfile.ZipFile(mem_zip, mode="w") as zf:
for filename, content in report_files.items():
zf.writestr(filename, content)
response = HttpResponse(mem_zip, content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename="{}"'.format(f'{ward}_cover_letters.zip')
return response
Все отлично работает, пока не застегнута часть. Я получаю сообщение об ошибке:
объект типа 'HttpResponse' не имеет len ()
Как мне go узнать об этом?