admin action to run xhtml2pdf on queryset


I have the following Admin action (in my file) which in designed to download pdf files for selected items. It seems to be working apart from the fact that it will only create and download a pdf for the first item in the queryset. I think the problem lies in the ‘return response’ line but I don’t know what else to use in its place. Any input would be great, I’m stomped!

class ReleaseAdmin(admin.ModelAdmin):
    def participant(self, obj):
        return str(obj.customer.last_name) + ", " + str(obj.customer.first_name)
    def training(self, obj):
        return str(

    def print_release(self, request, queryset):
        print (updated)
        for obj in queryset.all():
            firstname = obj.customer.first_name
            lastname = obj.customer.last_name
            nwta =

            data = {'order':order,'firstname': firstname, 'lastname': lastname, 'nwta':nwta,}
            pdf=release_render_to_pdf('accounts/pdf_template.html', data)
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "Release_%s_%s.pdf" %(lastname,nwta,)
            content="attachment; filename=%s" %(filename)
            response['Content-Disposition'] = content
    return response
            self.message_user(request, ngettext(
            '%d Relase Form was successfully printed.',
            '%d Relase Forms were successfully printed.',
            ) % updated, messages.SUCCESS)
    print_release.short_description="Print Release Form(s)"

    list_display = ('participant','release_status','release_date_submitted' ,'note' )
    actions = ['print_release']
    ordering = ('customer',)
    list_filter = ('customer__order__training_registered__training__name','customer__order__training_registered', 'customer__order__regstatus','release_status','release_date_submitted' ,'note')
    search_fields = ('participant','note')

You are correct - you’re creating a new HttpResponse each time through the loop, so that each time you’re processing a file you’re overwriting the previous one.

Also, my understanding is that you can’t return multiple files with a Content-Type: application/pdf. You might be able to build an HttpResponse with a Content-Type: multipart/mixed, with each fragment having a Content-Type: application/pdf.

Or, what might be easier would be to create a zip file containing all the pdfs and download them as a bundle. (The cloud-style services that I use allowing for multiple-file downloads, such as Google Drive, do it that way.)