how to serve downloadable zip file in django
I think what you want is to serve a file for people to download it. If that's so, you don't need to render the file, it's not a template, you just need to serve it as attachment using Django's HttpResponse:
zip_file = open(path_to_file, 'r')
response = HttpResponse(zip_file, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % 'foo.zip'
return response
FileResponse
is preferred over HttpResponse
for binary files. Also, opening the file in 'rb'
mode prevents UnicodeDecodeError
.
zip_file = open(path_to_file, 'rb')
return FileResponse(zip_file)