djangoダウンロードcsvファイルをリンク
質問
私は新しいベスタッフによるイタリア製です。第一があること。
例:ユーザがヒットし、提出ボタン形式で表示する必要のある成功のページにリンクしてダウンロードできます。この結果はexcelファイルです。を作成します出力ファイルを使用xlwtモジュール表示のページを個別なものでも同時に行います。
私:私は走django1.1.1windows XP*環境でpython2.6.また同様の問題 ができなかったです。
私の成功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'
ちなみexample.xls は両方の場所C:/example.xls および静的フォルダ
構造
- webdb
- 静的
- example.xls
- ダイ
- urls.py
- views.py
- models.py
- 静的
私はこの2つのモジュールです。を利用した場合、backup_to_csvで動作でdownlodsなしに直接リンクです。どうなるのかがすでに持っているファイルです。がある場合その他の方法が持っている店舗ファイルがあります。
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
解決
今では動作しますが、私は、CSVするために、Excel(.XLS)からファイルの拡張子を変更する必要がありました。
私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
他のヒント
あなたの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ファイルへのパスがハードコーディングされているので、私はあなたがそれを必要としないと思います。
コメントOfri Ravivで。あなたはそのあなたに
を与えていることを述べましたはTypeError:整数
であるUが二番目の[オプション]整数になるはずが、uが渡される「RB」
そのうちの2つのパラメータを渡しているのFileWrapperの作成中ためラッパー=のFileWrapper(ファイル(ファイル名)、 "RB")
実際のように記述する必要があります(「rbが」ファイルためのパラメータである)
ラッパー=のFileWrapper(ファイル(ファイル名、 "RB"))
だから、中括弧の単なるミスアライメントだったが、それは難しい、時にはデバッグできるようになります。