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"
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:
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