Django download csv file from the link

I am new to django and python. You need to be guided in this task.

Happening. When the user clicks the submit button on the form, he should display a success page and a link where they can upload the results. The results are in the excel file. I can create an excel output file using the xlwt module and display the success page separately, but not at the same time.

What I have: I am running django1.1.1 on Windows XP using python 2.6. A similar question was asked but could not get it to work.

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 a 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 is in both places C: /example.xls and in the static folder

Composition:

  • Webdb
    • Static
      • example.xls
    • Webinput
      • urls.py
      • views.py
      • models.py

I have these 2 modules. If I use backup_to_csv, it works fine, but it downlods directly without a link. How to do the same when I already have a file. If there are other ways when I do not need to store the file, this is also good.

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 (query, string):

 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
file django csv download xls
source share
3 answers

Now it works, but I had to change the 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 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 
+7
source share

In your urls.py change

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

to

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

In the first, you also pass everything after / to the view as another parameter, but your view does not accept this parameter. another option is to accept this parameter in the view:

 def send_file(request, path): ... 

but since the path to your xls file is hardcoded, I don't think you need it.

+2
source share

In the comments of Ofri Raviv. you mentioned that he gives you

TypeError: integer

because when creating FileWrapper u two parameters pass, of which the second [optional] must be an integer, but u is passed 'rb'

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

Actually it should be written as ('rb' is a parameter for the file)

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

So, it was just a mismatch of braces, but sometimes it’s hard to debug.

+1
source share

All Articles