Hi guys, i need help downloading results as csv, i am failing dismally I
dont know what to do
After the search results return values I want to download those results
into my computer somewhere the current version of my code is giving me an
error:
*<class 'TypeError'> string indices must be integers*
Here is my code
*FROM THE SEARCH VIEW:*
* <a href="{{=URL('download_results', args=request.vars)}}"
class="btn">Download Results as CSV</a>*
*DOWNLOAD CONTROLLER:*
*import csvfrom io import StringIOdef download_results(): # Retrieve the
filtered records records = request.vars # CSV data csv_data = []
csv_data.append(['First Name', 'Last Name', 'Program', 'Study Mode']) #
Header row for record in records:
csv_data.append([record['first_name'], record['last_name'],
record['program'], record['study_mode']]) # Create a response to
download the CSV file response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment;
filename=registration_results.csv' # Write CSV data to a StringIO
object output = StringIO() csv_writer = csv.writer(output) for row
in csv_data: csv_writer.writerow(row) # Return the content as
bytes return output.getvalue().encode('utf-8')*
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/a0d814f4-83b7-402a-8aa8-aa17528625a1n%40googlegroups.com.