小编Abe*_*edy的帖子

生成多个 PDF 并压缩它们以供下载,所有这些都在一个视图中

我正在使用xhtml2pdf在我的 Django 视图中生成 PDF。这个想法是遍历查询中存在的所有实例,然后为每个实例创建一个 PDF,然后将所有生成的 PDF 添加到一个 zip 文件中以供下载。该xtml2pdf逻辑是工作不错,但在循环逻辑是什么让我头疼。

所以这是我到目前为止的功能:

def bulk_cover_letter(request, ward_id, school_cat_id, cheque_number):
    school_type = SchoolType.objects.get(id=school_cat_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:
        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
        }

        response = HttpResponse('<title>Cover Letter</title>', content_type='application/pdf')
        filename = "%s.pdf" %(cheque_number)
        content = "inline; …
Run Code Online (Sandbox Code Playgroud)

django zipfile python-3.x xhtml2pdf

7
推荐指数
1
解决办法
1095
查看次数

标签 统计

django ×1

python-3.x ×1

xhtml2pdf ×1

zipfile ×1