views:

36

answers:

3

Hello I am generating tar.gz files with Django and save it to somewhere like /home/foo/foo.tar.gz but I don't know what is a good way to serve these generated files under django view.

I am using return HttpResponseRedirect("/home/foo/foo.tar.gz") but it is actually not a good way to serve tar.gz files because the generated tar.gz file path start from / (root dir) of my linux server instead of a relative path.

Thanks.

+3  A: 

Just send it in the response.

Ignacio Vazquez-Abrams
A: 

If you're not looking to protect with with authentication via Django, you can serve it up with your http server (nginx, lighttpd, apache, etc) - this reduces server impact.

James
A: 

You could define the redirect path relative to MEDIA_ROOT or another setting in settings - and as James says you should definitely consider configuring your http server to handle those files if you haven't already.

# settings.py
TARBALL_ROOT = '/home/foo/tarballs/'

# views.py
import os
from django.conf import settings

def your_view(request):
    # do some stuff
    filepath = os.path.join(settings.TARBALL_ROOT, 'relative/path/from/media/root'
    return HttpResponseRedirect(filepath)
Chris Lawlor