10

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

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

我所拥有的:我在 Windows XP 上使用 python 2.6 运行 django1.1.1。有人提出了类似的问题,但无法使其发挥作用。

我的成功 page.html 有这一行

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

网址.py:

url(r'^static/(?P<path>.*)$', send_file), 

视图.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
    • 网页输入
      • 网址.py
      • 视图.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
4

3 回答 3

10

Now it works but i had to change file extension from excel (.xls) to csv.

My urls.py=url(r'^static/example.txt', send_file)
My HTML link=<a href="../static/example.txt">Download CSV File</a>
My 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
于 2009-12-21T20:50:55.473 回答
3

在您的 urls.py 更改

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

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

在第一个中,您还将 / 之后的所有内容作为另一个参数传递给视图,但您的视图不接受此参数。另一种选择是在视图中接受此参数:

def send_file(request, path):
    ...

但由于您的 xls 文件的路径是硬编码的,我认为您不需要它。

于 2009-12-19T08:49:30.273 回答
1

在评论 Ofri Raviv。你提到它给你一个

类型错误:一个整数

这是因为在创建 FileWrapper 时,您传递了两个参数,其中第二个 [可选] 应该是整数,但您传递了 'rb'

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

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

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

所以这只是大括号的错位,但有时很难调试。

于 2012-08-15T08:15:25.390 回答