Reputation: 151
Hi I am having trouble displaying my list to the template. In the console it prints all. However when I do a print after the for loop, I only get the last line of the list.
Here is my code:
if 'process_udr' in request.POST:
#API Url for UDR History
apiUrl_udr ='http://apiurl'
#API Get Variable for UDR History
response_udr = urllib2.urlopen(apiUrl_udr)
reader = csv.reader(response_udr)
for udr_rows in reader:
print udr_rows
payload = {'usr': usr, 'usrpw': usrpw, 'response': response, 'response_2': response_2, 'response_udr': response_udr, 'udr_rows': udr_rows,}
return render_to_response(template, payload, context_instance=RequestContext(request))
else:
None
The results I see printed from the for loop in the console:
['09/18/14', '08:00:00', '09/18/14', '08:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '57565106', '0', '0', '19854']
['09/18/14', '09:00:00', '09/18/14', '09:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '71792666', '0', '0', '20994']
['09/18/14', '10:00:00', '09/18/14', '10:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '32902589', '0', '0', '30552']
['09/18/14', '11:00:00', '09/18/14', '11:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '46560924', '0', '0', '26552']
['09/18/14', '12:00:00', '09/18/14', '12:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '76349057', '0', '0', '52998']
['09/18/14', '13:00:00', '09/18/14', '13:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '77010644', '0', '0', '20510']
['09/18/14', '14:00:00', '09/18/14', '14:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '78546837', '0', '0', '15008']
['09/18/14', '15:00:00', '09/18/14', '15:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '79770873', '0', '0', '30726']
['09/18/14', '16:00:00', '09/18/14', '16:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '75913522', '0', '0', '23696']
['09/18/14', '17:00:00', '09/18/14', '17:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '19052209', '0', '0', '17656']
This is actually what's being printed after the for loop.
['09/18/14', '17:00:00', '09/18/14', '17:00:00', 'XXXXXXXXXXX', 'GRP-XXXXXXXXXXX', 'DEFAULT', 'spostmv', '19052209', '0', '0', '17656']
Here is my template code:
{% for udr_row in udr_rows %}
{{udr_row}}
{% endfor %}
Any help is greatly appreciated.
Upvotes: 1
Views: 173
Reputation: 174624
The problem is after you print it to the console with this loop:
for udr_rows in reader:
print udr_rows
The file pointer is at the end of the file - which is why nothing gets sent to your template. Convert the reader object directly to a list, and then send it to your template:
reader = csv.reader(response_udr)
payload = {'usr': usr,
'usrpw': usrpw,
'response': response,
'response_2': response_2,
'response_udr': response_udr,
'udr_rows': list(reader)}
You should also use the render
shortcut, and you don't need to send the response to your template; you should enable the request context processor and your templates will automatically get the request object.
This solution sort of works, but I need to grab each list, and not put everything into one list, if that makes sense. When it prints from the for, each is a list I believe. I want to do this so I can stylize the list into tables with headings
What you are getting in the template is a list-of-lists, so it would be very easy for you to create a table with it:
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
...
<th>Column N</th>
</tr>
</thead>
<tbody>
{% for row in udr_rows %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Upvotes: 2
Reputation: 21243
You are printing from for loop as
rows = []
for udr_rows in reader:
print udr_rows
rows.append(udr_rows)
so value of udr_rows
will be last line or reader
.
Please change your payload to get reader
instead of udr_rows
.
payload = {'usr': usr, 'usrpw': usrpw, 'response': response, 'response_2': response_2, 'response_udr': response_udr, 'udr_rows': rows,}
Upvotes: 1
Reputation: 2089
Put
udr_rows = reader
After the for...that should make the trick. You are printing only the last value on it
Upvotes: 2