Displaying a template table using a query set

2,354 views
Skip to first unread message

Pranav

unread,
Oct 20, 2010, 3:29:32 AM10/20/10
to Django users
I'm trying to display a table in template using the result retrieved
from a query set, but i get problem when i try to display the table
iterating through each row'<tr>' and column'<td>'

view file:
from django.shortcuts import get_object_or_404, render_to_response
from models import Organization

def startpage(request):
table_data = Organization.objects.all()
return render_to_response('display_table.html', {'table_data':
table_data})

display_table template file:
<table align="left">
{% for row in table_data %}
<tr align="center">
{% for value in row %}
<td>
{{ row }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>

when i try to run this i get an error saying the object is not
itreable for the second for loop. please provide a solution to this
issue...

Thanks and Regards
Pranav

Daniel Roseman

unread,
Oct 20, 2010, 7:13:55 AM10/20/10
to Django users
`value` is an Organisation instance, and model instances are not
iterable, as you could see by trying it in the shell.

You could try passing a values_list instead of a queryset:
table_data = Organization.objects.all().values_list()
as these are iterable.
--
DR.

Pranav

unread,
Oct 20, 2010, 7:58:11 AM10/20/10
to Django users
Hi Dan,
Thanks for the fix it worked.
I guess Organization.objects.all().values_list() returns a list.
But what if i want to refer to a particular field say Organization.id
inside my template?

like:
<table>
{% for row in table_data %}
<tr>
<td>
<a href = "..."> {{ row.Organization.id }} </a>
</td>
</tr>
<% endfor %}
</table>

I guess I'll have to pass another query set object to do this,
will this have any effect on the throughput of the database?
Is it possible to access cached result of the query set inside the
template?

Steve Holden

unread,
Oct 20, 2010, 8:24:26 AM10/20/10
to django...@googlegroups.com
The point is that normally you don't just want to iterate over the
columns of a retrieved row, since they have differences in meaning that
normally require differences in display formatting and the like as well.

Therefore the solution you offer above is more or less what you actually
do.

regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/

Pranav

unread,
Oct 20, 2010, 9:07:16 AM10/20/10
to Django users
Hi Steve,
I know its a bit odd to display only a column of a table, but
thats the only way i can display a href list and a full fledged table
next to each other.
I guess I'll just have to go with passing 2 query set objects....

Casey S. Greene

unread,
Oct 20, 2010, 9:13:24 AM10/20/10
to django...@googlegroups.com
Hi Pranav,

I think what Steve was saying is that if you go back to your original
template and view:

<table align="left">
{% for row in table_data %}
<tr align="center">
{% for value in row %}
<td>
{{ row }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>

and you know, for instance, what fields you want to display and how
(like your id field) that you don't need to first go to a list (this is
assuming the view you first posted, not the one that goes to a list):

<table align="left">
{% for row in table_data %}
<tr align="center">

<td><a href = "..."> {{ row.id }} </a></td>


</tr>
{% endfor %}
</table>

which is how you proposed to fix the list based situation the second
time (i.e. how you are thinking it should work is how django actually
works if you don't force it to go through the list step).

Hope this helps,
-- Casey

Reply all
Reply to author
Forward
0 new messages