django下载文件
views.py
from django.http import FileResponse def download(request): file = open('/root/Python-3.6.4.tgz', 'rb') response = FileResponse(file) response['Content-Type']='application/octet-stream' myname='test1.tgz' response['Content-Disposition']='attachment;filename="{0}"'.format(myname) return response
url.py添加
path('download/', download, name='file_down')
前端
<form action=""> <a href="{% url 'file_down' %}">下载python</a> </form>
参考:
努力生活,融于自然