开发者

how to serve generated files via django

I am generatin开发者_如何学Gog 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.


Just send it in the response.


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜