Reputation: 31
I have a zip file stored in the Django project directory. How can I serve it to users for downloading.
Upvotes: 1
Views: 2456
Reputation: 2269
You can serve any type of file using HttpResponse
, like so:
from django.http.response import HttpResponse
def zip_file_view(request):
response = HttpResponse(open('/path/to/your/zipfile.zip', 'rb'), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename=any_name_you_like.zip'
return response
Upvotes: 3
Reputation: 366
You want to serve it as a static file. You can find the steps to set up serving of static files here: https://docs.djangoproject.com/en/2.1/howto/static-files/
Be aware that serving static files in production (not using django's built in server) requires a few extra steps which are mentioned in the #Deployment section
Upvotes: 0