django使用链接下载csv文件

use*_*850 8 csv django xls file download

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

BTW example.xls位于C:/example.xls和静态文件夹中

结构体:

  • webdb
    • 静态的
      • example.xls
    • Webinput
      • 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
Run Code Online (Sandbox Code Playgroud)

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

use*_*850 9

现在它可以工作,但我不得不将文件扩展名从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 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
Run Code Online (Sandbox Code Playgroud)