Devendra Kushwah
Devendra Kushwah

Reputation: 31

How to serve a zip file download in Django?

I have a zip file stored in the Django project directory. How can I serve it to users for downloading.

Upvotes: 1

Views: 2456

Answers (2)

YellowShark
YellowShark

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

Ravi Patel
Ravi Patel

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

Related Questions