Sunday, March 31, 2013

Re: How do I get these field values in my template?

Al 31/03/13 15:18, En/na frocco ha escrit:
> cat = Product.objects.values_list('category__image_path','category__id')
>
> outputs;
> (u'category/goodyear logo.jpg', 13)
>
> I tried for c in cat:
> c.image_path
> c.id

As its name may suggest values_lists() returns (kind) a list of lists,
so you must index by position, not by field name:

for c in cat:
print c[0], c[1]

{% for c in cat %}
<p>{{ c.0 }} {{c.1}}</p>
{% endfor %}

If you want to index by field name you should use values(), which
returns a list of dictionaries mapping "field name" to "value".



HTH

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

No comments:

Post a Comment