On Oct 20, 4:13 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Oct 20, 8:29 am, Pranav <pranav...@gmail.com> wrote:
>
>
>
>
>
> > 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
>
> `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.

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?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to