2009-12-18 154 views
7

我是一个新的django和python。在这个任务中需要一些指导。django使用链接下载csv文件

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

我有什么: 我在Windows XP上用python 2.6运行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' 

BTW example.xls是在两个位置C:/example.xls中和静电夹

结构:

  • webdb
    • 静态
      • example.xls
    • Webinput
      • urls.py
      • views.py
      • models.py

我有这些2个模块以及。如果我使用backup_to_csv,它可以正常工作,但是直接在没有链接的情况下直接下载。当我已经有一个文件时如何做同样的事情。如果有其他方式我不需要存储文件,那也没关系。

高清xls_to_response(XLS,FNAME):

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

高清backup_to_csv(请求行):

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 

回答

7

现在,它的工作原理,但我不得不将文件扩展名从excel(.xls)更改为csv。

我的urls.py = url(r'^static/example.txt', send_file)
我的HTML链接= <a href="../static/example.txt">Download CSV File</a>
我view.py

def send_file(request): 

    import os, tempfile, zipfile 
    from django.core.servers.basehttp 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

在你的网址。PY 变化

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

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

在第一个,你也通过一切/到视图作为另一个参数后,但你的观点不接受此参数。另一种选择是在视图中接受这个参数:

def send_file(request, path): 
    ... 

但因为你的XLS文件的路径是硬编码,我不认为你需要的。

+0

谢谢,但它给这个错误 回溯(最近通话最后一个): 文件 “C:\ Python26 \ LIB \站点包\ Django的\核心\服务器\ basehttp.py”,线路280,在运行 self.finish_response() 文件 “C:\ Python26 \ LIB \站点包\ Django的\核心\服务器\ basehttp.py”,线路319,在finish_response 在self.result数据: 文件“C :\ Python26 \ lib \ site-packages \ django \ http \ __ init__.py“,第378行,在下一个 chunk = self._iterator.next() 文件”C:\ Python26 \ lib \ site-packages \ django \ core \ servers \ basehttp.py“,第50行,在下一个 data = self.filelike.read(self.blksize) TypeError:需要一个整数 – user234850 2009-12-21 16:50:53

1

在评论Ofri雷维吾。你提到它给你一个

TypeError: an integer

这是因为在创建FileWrapper u的传递两个参数外面第二个[可选]应该是整数但你通过了“RB”

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

这实际上应该写成( 'RB' 是文件中的参数)

包装= FileWrapper(文件(文件名, “RB”))

所以这只是一个misali大括号,但有时难以调试。