Hi,

I have the following models for each item in my shopping cart:

class CartItem(models.Model):
        option = models.ForeignKey(Option) #product option tied to a
particular product
        cart = models.ForeignKey(Cart) #contains all cart items
        quantity = models.IntegerField(blank=True, null=True)
        unit_price = models.FloatField(blank=True, null=True)

        def __unicode__(self):
                return (self.option.full_name + "=>" + str(self.quantity))


I'm trying to figure out how a clean way to build a form with newform
library to permit a user to modify quantities of each CartItem
instance in the shopping cart.


As a matter of fact, I don't know how many items are in the shopping
cart, so I tryed to do like this:


class FormForItemQuantity(forms.Form):

        def __init__(self, cart_items, *args, **kwargs):
                super(FormForItemQuantity, self).__init__(*args, **kwargs)
                for item in cart_items:
                        
self.fields[str(item.id)]=forms.IntegerField(initial=item.quantity)
                return

My view is therefore something like this:

def show_cart(request):
        cart = Cart.objects.get(pk=request.session.get('cart_id', None))
        cart_items = cart.cartitem_set.all() #Just to avoid two hits for the
same info
        form_set = FormForItemQuantity(cart_items)
        cart_rows = zip(cart_items, form_set)
        return render_to_response('show_cart3.html', {'cart': cart,
'num_items': cart.no_items, 'cart_items': cart_items,
'form_set':form_set, 'cart_rows':cart_rows})


and show_cart3 template is:

<h3>Your Cart</h3>
        <table>
                <tr>
                        <td>Prodotto</td>
                        <td>Variante</td>
                        <td>Quantità</td>
                        <td>Azione</td>
                </tr>
                <form method="POST" action="/carts/update_quantity/">
                         {% for item,form in cart_rows %}
                        <tr>
                                <td>{{item.option.product.full_name}}</td>
                                <td>{{item.option.full_name}}</td>
                                <td>{{form}}</td>
                                <td><a 
href="/carts/{{item.id}}/delete">Remove</a></td>
                        </tr>
                         {% endfor %}
                </table>
                <input type="SUBMIT" value="Aggiorna quantit&agrave;"/>
        </form> Numero items: {{num_items}}
        <a href="/carts/empty">Empty the cart</a> <br/> <a href="/orders/
save">Save the order</a>

The form is displayed as I wanted. However, I'm not able to clean
posted data.

How can I access the form and extract the quantity data posted from
the user?

I tried to something like:

quantity_form = FormForItemQuantity(request.POST)

but it isn't working.


Thank you for any help.


--~--~---------~--~----~------------~-------~--~----~
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