【问题标题】:django download csv file using a linkdjango 使用链接下载 csv 文件
【发布时间】:2010-12-28 05:00:21
【问题描述】:

我是 django 和 python 的新手。在这个任务中需要一些指导。

案例:当用户点击表单上的提交按钮时,它应该显示成功页面和一个可以下载结果的链接。结果在excel文件中。我可以使用 xlwt 模块创建输出到 excel 文件并单独显示成功页面,但不能同时显示两者。

我有什么: 我正在使用 python 2.6 在 Windows XP 上运行 django1.1.1。有类似的问题被问到 但无法使其工作。

我的成功 page.html 有这一行

<a href="../static/example.xls">Download CSV File</a>

urls.py:

url(r'^static/(?P<path>.*)$', send_file), 

views.py:

def send_file(request):

import os, tempfile, zipfile
from django.core.servers.basehttp import FileWrapper

"""                                                                         
Send a file through Django without loading the whole file into              
memory at once. The FileWrapper will turn the file object into an           
iterator for chunks of 8KB.                                                 
"""
filename = "C:/example.xls" # Select your file here.                                
wrapper = FileWrapper(file(filename),"rb")
response = HttpResponse(wrapper, content_type='text/plain')
#response['Content-Length'] = os.path.getsize(filename)
return response

当我点击链接时,它给出了路径错误

send_file() got an unexpected keyword argument 'path'
Request Method: GET
Request URL:    localhost:8000/webinput/static/example.xls
Exception Type: TypeError
Exception Value:    
send_file() got an unexpected keyword argument 'path'

顺便说一句,example.xls 位于 C:/example.xls 和静态文件夹中

结构:

  • 网络数据库
    • 静态
      • example.xls
    • 网页输入
      • urls.py
      • views.py
      • models.py

我也有这两个模块。如果我使用 backup_to_csv 它工作正常,但它直接下载而没有链接。当我已经有文件时如何做同样的事情。如果有其他方式我不必存储文件,那也很好。

def xls_to_response(xls, fname):

response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % fname
xls.save(response)
return response

def backup_to_csv(request,row):

response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename="backup.csv"'
writer = csv.writer(response, dialect='excel')    
#code for writing csv file go here...
for i in row:
    writer.writerow(i)
return response

【问题讨论】:

    标签: django file csv download xls


    【解决方案1】:

    现在它可以工作了,但我必须将文件扩展名从 excel (.xls) 更改为 csv。

    我的 urls.py=url(r'^static/example.txt', send_file)
    我的 HTML 链接=&lt;a href="../static/example.txt"&gt;Download CSV File&lt;/a&gt;
    我的观点.py

    def send_file(request):
    
      import os, tempfile, zipfile
      from wsgiref.util import FileWrapper
      from django.conf import settings
      import mimetypes
    
      filename     = "C:\ex2.csv" # Select your file here.
      download_name ="example.csv"
      wrapper      = FileWrapper(open(filename))
      content_type = mimetypes.guess_type(filename)[0]
      response     = HttpResponse(wrapper,content_type=content_type)
      response['Content-Length']      = os.path.getsize(filename)    
      response['Content-Disposition'] = "attachment; filename=%s"%download_name
      return response
    

    【讨论】:

      【解决方案2】:

      在你的 urls.py 改变

      urls.py url(r'^static/(?P.*)$', send_file)
      

      urls.py url(r'^static/example.xls$', send_file)
      

      在第一个中,您还将 / 之后的所有内容作为另一个参数传递给视图,但您的视图不接受此参数。另一种选择是在视图中接受此参数:

      def send_file(request, path):
          ...
      

      但由于您的 xls 文件的路径是硬编码的,我认为您不需要它。

      【讨论】:

      • 谢谢,但它给出了这个错误 Traceback(最近一次调用最后):文件“C:\Python26\lib\site-packages\django\core\servers\basehttp.py”,第 280 行,在运行 self.finish_response() 文件“C:\Python26\lib\site-packages\django\core\servers\basehttp.py”,第 319 行,在 self.result 中的数据的 finish_response:文件“C:\Python26\lib \site-packages\django\http_init_.py",第 378 行,在下一个块 = self._iterator.next() 文件“C:\Python26\lib\site-packages\django\core \servers\basehttp.py", line 50, in next data = self.filelike.read(self.blksize) TypeError: an integer is required
      【解决方案3】:

      在 cmets Ofri Raviv。你提到它给你一个

      类型错误:一个整数

      这是因为在创建 FileWrapper 时,你传递了两个参数,其中第二个 [可选] 应该是整数,但你传递了 'rb'

      wrapper = FileWrapper(file(filename),"rb")

      实际上应该写成('rb'是File的参数)

      wrapper = FileWrapper(file(filename,"rb"))

      所以这只是大括号的错位,但有时很难调试。

      【讨论】:

        猜你喜欢
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 2017-03-28
        相关资源
        最近更新 更多