Home >Backend Development >Python Tutorial >Detailed explanation of three ways to download files in Django_python

Detailed explanation of three ways to download files in Django_python

不言
不言Original
2018-04-08 11:22:192347browse

This article mainly introduces the three ways to download files in Django. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

1. Overview

In actual projects, the download function is often needed. , such as importing excel, pdf or file downloads. Of course, you can use web services to build your own resource servers that can be used for downloading, such as nginx. Here we mainly introduce file downloads in Django.

Implementation method: a tag + response header information (of course you can choose form implementation)

<p class="col-md-4"><a href="{% url &#39;download&#39; %}" rel="external nofollow" >点我下载</a></p>

Method 1: Use HttpResponse

Routing url:

url(r&#39;^download/&#39;,views.download,name="download"),

views.py code

from django.shortcuts import HttpResponse
def download(request):
  file = open(&#39;crm/models.py&#39;, &#39;rb&#39;)
  response = HttpResponse(file)
  response[&#39;Content-Type&#39;] = &#39;application/octet-stream&#39; #设置头信息,告诉浏览器这是个文件
  response[&#39;Content-Disposition&#39;] = &#39;attachment;filename="models.py"&#39;
  return response

Method 2: Use StreamingHttpResponse

Other logic remains unchanged, the main changes are in the back-end processing

from django.http import StreamingHttpResponse
def download(request):
  file=open(&#39;crm/models.py&#39;,&#39;rb&#39;)
  response =StreamingHttpResponse(file)
  response[&#39;Content-Type&#39;]=&#39;application/octet-stream&#39;
  response[&#39;Content-Disposition&#39;]=&#39;attachment;filename="models.py"&#39;
  return response

Method Three: Using FileResponse

from django.http import FileResponse
def download(request):
  file=open(&#39;crm/models.py&#39;,&#39;rb&#39;)
  response =FileResponse(file)
  response[&#39;Content-Type&#39;]=&#39;application/octet-stream&#39;
  response[&#39;Content-Disposition&#39;]=&#39;attachment;filename="models.py"&#39;
  return response

##Usage Summary

Three http response objects are introduced on the django official website. Entrance: https://docs.djangoproject.com/en/1.11/ref/request-response/

It is recommended to use FileResponse. It can be seen from the source code that FileResponse is a subclass of StreamingHttpResponse and uses iterators internally for data streaming.


The above is the detailed content of Detailed explanation of three ways to download files in Django_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn