Malcolm, Carl and Felix,

Thanks for the help!  I figured out a solution to my above problem by
using the suggested for loop approach to build the tuple of products
and forms.  It may not be the most elegant python code, but it is
working and readable.

# Build a tuple of products and forms for each product
zipped = []
count = 0
for form in formset.forms:
    product = product_queryset[count]
    zipped.append( ( form, product) ) # add them as a tuple
    count = count + 1

Thanks!

On Sep 30, 12:36 pm, SnappyDjangoUser <[EMAIL PROTECTED]> wrote:
> This is extremely helpful!  I didn't know this was possible in Django
> to create a tuple and loop through both items using a for loop in the
> template.  Thank you!
>
> I am currently hung up on one last part, however.  In the example
> above you assume that both p and v belong to products.  In my case,
> that is not the case.  I have a queryset of products that contains the
> vendor name, model name and model number.  I also have a formset
> containing forms.  I am trying to combine these 2 lists of objects
> into the tuple so I can iterate over both in the template.
>
>     # or more pythonically if its easy to find your vendor:
>     zipped = [ (form, << NEED HELP HERE product_queryset>>) for form
> in formset.forms ]
>     context = { 'products': zipped }
>
>  {% for form, product in products %}
>    {{product.vendor}} {{product.model_name}} {{product.model_num}}
> {{form}}
>  {% endfor %
>
> Any suggestions?
>
> On Sep 30, 9:45 am, felix <[EMAIL PROTECTED]> wrote:
>
> > malcom is suggesting this:
>
> > def view(request):
> >     blah blah blah
> >     ...
> >     zipped = []
> >     for p in products:
> >       v = find the vendor for this product
> >       zipped.append( ( p, v) ) # add them as a tuple
>
> >    # or more pythonically if its easy to find your vendor:
> >    zipped = [ (p, vendor for product) for p in products ]
> >    context = { 'products': zipped }
>
> > {% for product, vendor in products %}
> >   {{product}} {{vendor}}
> > {% endfor %}
>
> > On Sep 30, 5:05 pm, SnappyDjangoUser <[EMAIL PROTECTED]> wrote:
>
> > > Hi Malcolm,
>
> > > You suggested:
>
> > > > 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).
>
> > > This is exactly what I like to do!  I am still a bit confused on exact
> > > implementation, however, because I have not found a way in Django to
> > > loop through 2 structures at once in a template.  The forloop.counter
> > > seems that it is mostly used for printing the iteration through the
> > > loop (not for indexing) and the "for loop" tag in Django is built only
> > > to iterate through one structure at a time.  Do you have any examples
> > > how how to loop through 2 strucutres simultaneously?
>
> > > Thanks!
>
> > > -Brian
>
> > > On Sep 30, 12:10 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
> > > wrote:
>
> > > > 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to