django下载csv文件使用链接 [英] django download csv file using a link
问题描述
我是django和python的新手。在此任务中需要一些指导。
I am a new to django and python. Need some guidance in this quest.
案例:当用户点击表单上的提交按钮时,应显示成功页面以及可以下载结果的链接。结果在excel文件中。我可以使用xlwt模块创建输出到excel文件,并单独显示成功页面,但不能同时显示两者。
Case: When the user hits the submit button on a form, it should display Success page and a link where they can download the results. The results are in excel file. I can create output to excel file using xlwt module and display the success page individually but not both at the same time.
我有:
我使用python 2.6在Windows XP上运行django1.1.1。有类似的问题
,但不能使它工作。
What i have: I am running django1.1.1 on windows XP with python 2.6. There was similar question asked but was not able to make it work.
我的成功page.html有这一行
my success page.html has this line
<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
当我点击链接给出路径错误
When i click on the link, it gives path error
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和静态文件夹
BTW example.xls is at both the locations C:/example.xls and in static folder
结构:
- webdb
- 静态
- example.xls
- urls.py
- views.py
- models.py
如果我使用backup_to_csv它工作正常,但它downlods直接没有链接。如何做同样当我已经有一个文件。如果还有其他方法,我不必存储文件,这也很好。
I have these 2 modules as well. If i use backup_to_csv it works fine but it downlods directly without the link. How to do the same when i already have a file. If there are other ways where i dont have to store file, that is fine too.
def xls_to_response(xls,fname):
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(request,row):
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
推荐答案
现在它工作,但我不得不更改文件扩展名从excel(.xls)到csv。
Now it works but i had to change file extension from excel (.xls) to csv.
我的urls.py =
url(r'^ static / example.txt',send_file)
我的HTML链接=< a href =../ static / example .txt>下载CSV文件< / a>
我的view.pyMy urls.py=
url(r'^static/example.txt', send_file)
My HTML link=<a href="../static/example.txt">Download CSV File</a>
My view.pydef send_file(request): import os, tempfile, zipfile from django.core.servers.basehttp 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文件使用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 静态