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>


参考:

https://blog.csdn.net/supery071/article/details/108790574

https://www.cnblogs.com/huaibin/p/12752522.html

生命的意义在于奉献, 解决各种问题
原文地址:https://www.cnblogs.com/regit/p/14850681.html