2009-12-18 151 views
7

我是一個新的django和python。在這個任務中需要一些指導。django使用鏈接下載csv文件

案例:當用戶點擊表單上的提交按鈕時,應顯示成功頁面和鏈接,他們可以在其中下載結果。結果在excel文件中。我可以使用xlwt模塊創建輸出到excel文件,並單獨顯示成功頁面,但不能同時顯示。

我有什麼: 我在Windows XP上用python 2.6運行django1.1.1。有類似的問題要求 ,但無法使其工作。

我的成功page.html中有這條線

<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 

當我點擊鏈接,它給路徑錯誤

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中和靜電夾

結構:

  • webdb
    • 靜態
      • example.xls
    • Webinput
      • urls.py
      • views.py
      • models.py

我有這些2個模塊以及。如果我使用backup_to_csv,它可以正常工作,但是直接在沒有鏈接的情況下直接下載。當我已經有一個文件時如何做同樣的事情。如果有其他方式我不需要存儲文件,那也沒關係。

高清xls_to_response(XLS,FNAME):

response = HttpResponse(mimetype="application/ms-excel") 
response['Content-Disposition'] = 'attachment; filename=%s' % fname 
xls.save(response) 
return response 

高清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 

回答

7

現在,它的工作原理,但我不得不將文件擴展名從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 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 
2

在你的網址。PY 變化

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

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

在第一個,你也通過一切/到視圖作爲另一個參數後,但你的觀點不接受此參數。另一種選擇是在視圖中接受這個參數:

def send_file(request, path): 
    ... 

但因爲你的XLS文件的路徑是硬編碼,我不認爲你需要的。

+0

謝謝,但它給這個錯誤 回溯(最近通話最後一個): 文件 「C:\ Python26 \ LIB \站點包\ Django的\核心\服務器\ basehttp.py」,線路280,在運行 self.finish_response() 文件 「C:\ Python26 \ LIB \站點包\ Django的\核心\服務器\ basehttp.py」,線路319,在finish_response 在self.result數據: 文件「C :\ Python26 \ lib \ site-packages \ django \ http \ __ init__.py「,第378行,在下一個 chunk = self._iterator.next() 文件」C:\ Python26 \ lib \ site-packages \ django \ core \ servers \ basehttp.py「,第50行,在下一個 data = self.filelike.read(self.blksize) TypeError:需要一個整數 – user234850 2009-12-21 16:50:53

1

在評論Ofri雷維吾。你提到它給你一個

TypeError: an integer

這是因爲在創建FileWrapper u的傳遞兩個參數外面第二個[可選]應該是整數但你通過了「RB」

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

這實際上應該寫成( 'RB' 是文件中的參數)

包裝= FileWrapper(文件(文件名, 「RB」))

所以這只是一個misali大括號,但有時難以調試。