django 使用链接下载 csv 文件

标签 django file csv download xls

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

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

我有什么: 我在 Windows XP 上使用 python 2.6 运行 django1.1.1。有人问过类似的问题 但无法使其发挥作用。

我的成功 page.html 有这一行

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

url.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 和静态文件夹中

结构:

  • 网络数据库
    • 静态
      • 示例.xls
    • 网页输入
      • url.py
      • views.py
      • 模型.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(请求,行):

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

最佳答案

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

我的urls.py= url(r'^static/example.txt', send_file)
我的 HTML 链接= <a href="../static/example.txt">Download CSV File</a>
我的观点.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

关于django 使用链接下载 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1930983/

相关文章:

r - 如何将数值读取为 R 中的因子?

python - 对 CSV 列进行平均

django - Cronjob 无法在 webfaction 上工作

javascript - 在 JavaScript 警报中使用 PHP 从文件中收集的数据

python - 在 Django 中,放置 HTML 格式数据的简短片段的最佳位置在哪里?

c - 段错误,有大的难以理解的错误?

java - 通过修改desktop.ini来更改图标的文件夹

python - 使用 python 删除重复的 CSV 条目

python - Django 计数聚合与相关字段上的过滤器不起作用

python - 使用 Google App Engine 进行本地 Django 单元测试的 stub