2016-09-08 6 views
1

でファイルをダウンロードすることはできませんstackoverflowの上の回答のカップルを経て、これを試してみた:は、私がDjango.Iでファイルをダウンロードする方法を把握しようとしていますジャンゴ

views.py

def download(): 

    file = open("DemoCSV.csv", "r") 

    response = HttpResponse(file,content_type='application/vnd.ms-excel') 
    response['Content-Disposition'] = 'attachment; filename="DemoCSV.csv"' 
    return response 

ファイルDemoCSV.csvは私のアプリケーションと同じフォルダにあります。

私は、ブラウザからURLを打つとき、私はfile.Thisのエラーメッセージが表示されダウンロードすることができません。

TypeError at /resources/download_files 
download() takes 0 positional arguments but 1 was given 

私は何をしないのですか?

答えて

1

downloadはビューであるため、単一のパラメータであるHttpRequestオブジェクトが必要です。次のように変更してください

def download(request): 

    file = open("DemoCSV.csv", "r") 

    response = HttpResponse(file,content_type='application/vnd.ms-excel') 
    response['Content-Disposition'] = 'attachment; filename="DemoCSV.csv"' 
    return response 
+0

ありがとうございました。 –

+0

助けてくれてうれしい – e4c5

+0

また、この質問を見ている人は、 "open"ステートメントでファイルのフルパスを入力してください。 –

関連する問題