我是一个新的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
谢谢,但它给这个错误 回溯(最近通话最后一个): 文件 “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