You assign to context every loop iteration. So at the end, only the last
add_product_form constructed is ever used. Try this:

def show_cart(request):
    cart_id = _cart_id(request)
    cart_items = CartItem.objects.filter(cart_id=cart_id)
    add_product_forms = {}
    for item in cart_items:
        q = item.quantity
        print q    #shows correct value (quantity) of each item in cart
        add_product_form = ProductAddToCartForm(initial={'quantity': q})
        add_product_forms[item.pk] = add_product_form
    context = {'cart_items': cart_items, 'add_product_forms':
add_product_forms}
    return render(request, 'cart_id/cart.html', context)

Note how I moved the context assignment OUTSIDE the for loop. Then adjust
your template to index into add_product_forms based on item.pk.

There are lots of other ways to do this too, I just thought this was the
quickest to show over email.

On Sat, May 14, 2016 at 8:42 PM, Chris Kavanagh <cka...@gmail.com> wrote:

>
> I'm working on an online shop, and I'm having a problem in the cart,
>  here's a screen shot (http://pasteboard.co/Vwnv2T0.png).
> I'd like the form for each item (product) to show it's current quantity
> (that's already been added to the cart)
>  as an initial value as most ecommerce sites do.
>
> I've tried iterating through the items in the cart (in views.show_cart)
> then adding the form,
> however the form for all items in the cart shows the exact same value (of
> the last item).
> In other words, there's 2 items in the cart, one has a quantity of 7 and
> the other a quantity of 3,
>  but both forms show the quantity as 3.
>
>  I can't figure out how to correct this, although I'm sure it's a simple
> fix.
> Thanks in advance for the help, and I hope this question is formatted
> correctly?
> Here's the code on github if it helps. . .
> https://github.com/chriskavanagh/ecommerce/tree/master/src
>
> # form
> PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)]
> class ProductAddToCartForm(forms.Form):
>     quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES,
> coerce=int)
>     update =
> forms.BooleanField(required=False,initial=False,widget=forms.HiddenInput(attrs={'class':
> 'form-control'}))
>
>
> # views (cart_id.views.show_cart)
> def show_cart(request):
>     cart_id = _cart_id(request)
>     cart_items = CartItem.objects.filter(cart_id=cart_id)
>     for item in cart_items:
>         q = item.quantity
>         print q    #shows correct value (quantity) of each item in cart
>         add_product_form = ProductAddToCartForm(initial={'quantity': q})
>         context = {'cart_items': cart_items, 'add_product_form':
> add_product_form}
>     return render(request, 'cart_id/cart.html', context)
>
>
> # template (cart.html)
> {% extends 'base.html' %}
> {% load staticfiles %}
>
> {% block title %}Shopping Cart{% endblock %}
>
> {% block content %} {% endblock %}
>
> {% block extra_content %}
>     <div class="container">
>         <div class="col-md-12">
>
>         <h2>Cart</h2>
>         <caption><b>Your Shopping Cart:</b> {{ cart_items_count }} Items
> In Your Cart.</caption>
>         <hr>
>
>         {% for item in cart_items %}
>
>             <table class="table table-striped">
>                 <thead>
>                     <tr>
>                         <th>Product</th>
>                         <th>Unit Price</th>
>                         <th>Quantity</th>
>                         <th>Total Price</th>
>                     </tr>
>                 </thead>
>                 <tbody>
>                     <tr>
>                         <td><a href="{{ item.product.get_absolute_url
> }}">{{ item.product }}</a></td>
>                         <td>${{ item.product.price }}</td>
>                         <td>{{ item.quantity }}</td>
>                         <td>{{ item.total }}</td>
>                         <td>
>                         <div class="form-group">
>                             <form action="{% url 'cart:add'
> item.product.slug %}" method="POST">
>                                     {% csrf_token %}
>                                 <div class="col-xs-3">
>                                     {{ add_product_form.quantity }}
>                                 </div>
>                                     {{ add_product_form.update
> }}
>                                 <input class='btn btn-success btn-sm'
> type='submit' value='Update'/>
>                             </form>
>                         </div>
>                         </td>
>                     </tr>
>                 </tbody>
>             </table>
>
>         {% empty %}
>             <h3>You Have Nothing In Your Cart.</h3>
>         {% endfor %}
>     </div>
> </div>
>
> {% endblock %}
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/adb6c8ab-77bb-46ba-bec6-21df4e5499b8%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/adb6c8ab-77bb-46ba-bec6-21df4e5499b8%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxXmnDsqPMBZ33ANbDdLy51NPrNPnG-rrCreqprXPFbiRw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to