A ZipFile subclass that accepts Django Templates
Project description
A subclass of zipfile.Zipfile that works with Django templates.
Usage:
from zipfile import ZIP_DEFLATED from django_zipfile import TemplateZipFile def myview(request, object_id): obj = get_object_or_404(MyModel, pk=object_id) context = { 'object': obj } response = HttpResponse(mimetype='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=myfile.zip' container = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root='myapp/myzipskeleton/') container.add_template('mimetype') container.add_template('META-INF/container.xml') container.add_template('chapter1.html', context=context) container.close() return response
You can also specify multiple roots for your templates:
container = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root=['myapp/myzipskeleton/override/', 'myapp/myzipskeleton/default/'])
as well as multiple templates when you add files:
container.add_template(['override.html', 'default.html'], filename="chapter1.html")
TemplateZipFile will look for templates in the specified order, first by root, then by template name. For example:
myzipfile = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root=['override/', 'default/']) myzipfile.add_template(['two.txt', 'one.txt'], filename='myfile.txt') # Will use the first existing template from ['override/two.txt', 'override/one.txt', 'default/two.txt', 'default/one.txt']
If none of the templates can be found, add_template will raise a TemplateDoesNotExist error.
You can specify a file as optional with optional=True
myzipfile.add_template(['two.txt', 'one.txt'], filename='myfile.txt', optional=True)
Doing so will silently swallow the TemplateDoesNotExist exception.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-zipfile-0.0.8.tar.gz
(3.6 kB
view details)
File details
Details for the file django-zipfile-0.0.8.tar.gz
.
File metadata
- Download URL: django-zipfile-0.0.8.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a6c2a16e75abb363a55b73824ff29883f5fbfd63008464f6ca5b79d0467b9de |
|
MD5 | b3f010d82c7c1258dd757604e910017d |
|
BLAKE2b-256 | bd91192f9a35be9918ad5ffd8b0114a9bc7788085c9068115d68b2b606e83f40 |