Hi, I'm trying to figure out how to build a form to display x fields, where x is determined at runtime (e.g. a form to update the quantities of all the items in a shopping cart).
My model is: class CartForm(forms.Form): def __init__(self, c, *args, **kwargs): super(CartForm, self).__init__(*args, **kwargs) # c is a dictionary containing cart items for key in c.keys(): self.fields[str(key)]=forms.CharField(initial=c[key]) return My view is: 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 = CartForm(c=create_cart_dictionary(cart_items)) return render_to_response('show_cart3.html', {'cart': cart, 'num_items': cart.no_items, 'cart_items': cart_items, 'form':form}) def update_quantity(request): if request.method == 'POST': cart = Cart.objects.get(pk=request.session.get('cart_id', None)) form = CartForm(request.POST) #old_quantity =item.quantity if form.is_valid(): for item in cart.cartitem_set.all(): itemid = item.id new_quantity = form.cleaned_data['cart_item' + str(itemid)] ... save the new quantities Create_cart_dictionary is a function that create a dictionary like {'item_id': item}. show_cart() works as expected, since it displays a dynamically generated form with fields named by the item.id displayed. update_quantity() do not works, since form.is_valid() is always false: >>> form = CartForm({'a': 10, 'b':20}) >>> form <sensfly.carts.models.CartForm object at 0x13bce70> >>> form.is_valid() False >>> form = CartForm({'a': '10', 'b':'20'}) >>> form.is_valid() False >>> print form <tr><th><label for="id_a">A:</label></th><td><input type="text" name="a" value="10" id="id_a" /></td></tr> <tr><th><label for="id_b">B:</label></th><td><input type="text" name="b" value="20" id="id_b" /></td></tr> >>> form.is_bound False The problems seems to be that the form is not bound. What I'm doing wrong? How to bound and validate the dynamically generated form? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---