[英]How to serve a zip file download in Django?
我有一个zip文件存储在Django项目目录中。 如何将其提供给用户进行下载。
您可以使用HttpResponse
提供任何类型的文件,如下所示:
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
您要将其作为静态文件提供。 您可以在此处找到设置静态文件的步骤: https : //docs.djangoproject.com/en/2.1/howto/static-files/
请注意,在生产环境中提供静态文件(不使用django的内置服务器)需要一些额外的步骤,请在#Deployment部分中进行说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.