On Tuesday, August 22, 2023 at 2:32:48 AM UTC-7 mostwanted wrote:
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*
Isn't request.vars an array of strings?
* # 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']])*
If request.vars, and hence records, is an array of strings, then each record
is a string, and you're trying to index into that string with 'f*irst_name*',
etc.
You either need to repeat the search using request.vars, or pass the
records that the search got from request.vars.to download_results()
* # 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')*
/dps
--
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/93ed5077-9868-4659-800f-c7ced7dcda12n%40googlegroups.com.