On Mon, 2008-09-29 at 17:31 -0700, SnappyDjangoUser wrote: > Hi Folks, > > How can I use a forloop counter to index into a query set as in the > example below? > > (I know this code does not work, but I want to do something of the > sort): > > {% for form in quote_product_formset.forms %} > <tr> > <td>{{ product.(forloop.counter).Vendor }}</td> > </tr> > > {{ form }} > {% endfor %} > > (in the above case, Vendor is a field in product. So if I want to > access the first product when forloop counter is 1, the variable would > expland to "{{ product.1.Vendor }}")
You can't do indirect variable references like this in Django's templating language. The reasoning is that it ends up complicating the template structure when you wander down that path. Instead, factor it out into a template tag (that can be passed the current context, so it will have access to the forloop counter) or set up the data structures you pass to your view a bit differently so that you can loop over the forms and the products simultaneously (that is, pull apart the formset forms and zip them together with the product entries in the view). Usually the second approach works a bit more nicely, but either is possible. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---