Frank,
thak you for your help.

I worked on the form model using bound field, and now the forms is
bound.

class CartForm(forms.Form):
        def __init__(self, a):
                super(CartForm, self).__init__(a)
                for key in a.keys():
                        self.fields['cart_item_' + str(key)] = 
forms.IntegerField(required
= True)
                        self.fields['cart_item_' + str(key)].bf = 
BoundField(self,
self.fields['cart_item_' + str(key)], 'cart_item_' + str(key))
                        self.fields['cart_item_' + str(key)].bf._data = 
int(a[key])
                return


However, I still have some problems.
1- I don't understand how the form validates:

>>> f = CartForm({'a':'1', 'b':'2'})
>>> f.is_bound
True
>>> f.is_valid()
False
>>> k = CartForm({'a':1, 'b':2})
>>> k.is_bound
True
>>> k.is_valid()
False

I supposed the first one validation to be false since the field widget
is an IntegerField.


2 - I'm not able to figure out to read the form after it has been
posted.

Can you help me?

Many thanks




On 7 Ott, 02:45, FrankW <[EMAIL PROTECTED]> wrote:
> In your example,
>     self.fields[str(key)]=forms.CharField(initial=c[key])
> isn't doing what you expect.
>
> You are setting the initial attribute of a field, not creating
> a BoundField. See django/newforms/forms.py, and look at the
> comments for BaseForm and Form and the code for
> BoundField.
>
> On Oct 6, 8:31 am, pinco <[EMAIL PROTECTED]> wrote:
>
> > 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 [email protected]
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