小编use*_*850的帖子

django使用链接下载csv文件

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

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

我有:我在Windows XP上使用python 2.6运行django1.1.1.有类似的问题,但无法使其工作.

我的成功page.html有这一行

<a href="../static/example.xls">Download CSV File</a>
Run Code Online (Sandbox Code Playgroud)

urls.py:

url(r'^static/(?P<path>.*)$', send_file), 
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

send_file() got an unexpected keyword …
Run Code Online (Sandbox Code Playgroud)

csv django xls file download

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

csv ×1

django ×1

download ×1

file ×1

xls ×1