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

posted @ 2021-06-04 18:00  坚强的小蚂蚁  阅读(104)  评论(0编辑  收藏  举报