CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=17237cb837ab437c9e234803; HttpOnly; Path=/; Secure
set-cookie: trac_session=0db8197aca2db323d71b3f3d; expires=Wed, 29 Oct 2025 04:15:48 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Thu, 31 Jul 2025 04:15:49 GMT
x-served-by: cache-fra-eddf8230071-FRA, cache-bom-vanm7210074-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753935349.863202,VS0,VE240
vary: Accept-Encoding
CookBookDynamicZip – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Return a dynamically created zip file in your HttpResponse.
import zipfile from cStringIO import StringIO from django.utils.httpwrappers import HttpResponse def view_that_returns_zipped_file(request): response = HttpResponse(mimetype='application/zip') response['Content-Disposition'] = 'filename=all_things.zip' #first assemble your files files = [] for thing in Thing.objects.all(): files.append(("%s.pdf" % (thing.id,), thing.biggish_file())) #now add them to a zip file #note the zip only exist in memory as you add to it buffer = StringIO() zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) for name, f in files: zip.writestr(name, f) zip.close() buffer.flush() #the import detail--we return the content of the buffer ret_zip = buffer.getvalue() buffer.close() response.write(ret_zip) return response
Last modified
19 years ago
Last modified on Aug 21, 2006, 7:11:18 AM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.