django tar generation on request
Greetings, By using Django, I want to generate a tar.gz which contains the files below, and render a tar.gz file:
Content from an Ubuntu server:
/home/user/myfolder
/home/user/image.jpg
an xml file from www.mysite.com/foo.xml (which is a dynamicly generated xml file.
Rendered tar.gz file
myfolder/
content/
.....
image.jpg开发者_开发百科
foo.xml
What would be the best way to do this inside achieve this as a Django view?
The tarfile module is probably a good place to start.
You would probably want to cache the file somewhere in the filesystem to save yourself from too much work. There's no reason to regenerate the file if the contents haven't changed.
You can use django-tarview (install via pip
from PyPi, or get it from github)
Extend tarview.views.BaseTarView
and implement get_files
:
from tarview.views import BaseTarView
class TarView(BaseTarView):
def get_files(self):
return [File(open('test_file.txt'), name="test_file.txt"),
ContentFile(b"I am a text file", name="text.txt")]
精彩评论