acces cleaned data from a dynamic form

45 views
Skip to first unread message

het.oosten

unread,
Jun 24, 2012, 10:36:27 AM6/24/12
to Django users
Django 1.2.3

For a restaurant i want to make a form, were the user can add their
address, and their order. The order part is dynamic (users can add
extra fields). I used jquery.formset for this.

To accomplish this i have an address form and a dishes formset:

class Address(models.Model):
name = models.CharField(max_length=20)

class AddressForm(ModelForm):
class Meta:
model = Address

class DishesForm(forms.Form):
dish = forms.CharField(max_length=200)

DishesFormset = formsets.formset_factory(DishesForm)

In the view i call these forms with: form = AddressForm() formset =
DishesFormset()

I have two problems with the formset:

1. in the template i can only get a form with {{formset}} .
{{formset.dish}} doesn't work
2. how do i access the cleaned_data from the formset? When I test I
see in the POST that multiple dishes are posted, but I can only get
the first one in the cleaned_data

Rob

Daniel Roseman

unread,
Jun 25, 2012, 4:45:29 AM6/25/12
to django...@googlegroups.com
I don't understand your questions.

A formset is a set of forms. What would {{ formset.dish }} even refer to? Only the forms inside the formset have a `dish` field.

Question 2 is impossible to answer without seeing your view code. 
--
DR.

het.oosten

unread,
Jun 25, 2012, 6:52:37 AM6/25/12
to Django users
Thank you for your help!

> I don't understand your questions.

I am sorry about that, I probably ask my question the wrong way
because of my lack of experience

> A formset is a set of forms. What would {{ formset.dish }} even refer to?
> Only the forms inside the formset have a `dish` field.

When I try {% for form in formset %}{{form.dish}}

I get a "non iterable" error

In order to style the form right I need to call the seperate
formfields.

>
> Question 2 is impossible to answer without seeing your view code.
> --
> DR.

My views.py is very basic so not much to show (see below). When I view
the POST info all information is posted right. One address, Dish1,
Dish2 The only thing i want is validate the form and show/mail the
results. When i try "for form in formset.cleaned_data" i get that "non
iterable" error again.

Till now i only managed to validate and show Dish1. I probably oversee
something very basic.

def testform(request):
if request.method == 'POST':
form = AddressForm(request.POST)
formset = DishesFormset(request.POST)
if formset.is_valid():
return render_to_response('test', {
'form_data': formset.cleaned_data,
})
else:
form = AddressForm()
formset = DishesFormset()
return render_to_response('testform', {
'form': form,
'formset': formset,
}, context_instance = RequestContext(request))

Melvyn Sopacua

unread,
Jun 25, 2012, 7:09:24 AM6/25/12
to django...@googlegroups.com
On 25-6-2012 12:52, het.oosten wrote:
> Thank you for your help!
>
>> I don't understand your questions.
>
> I am sorry about that, I probably ask my question the wrong way
> because of my lack of experience
>
>> A formset is a set of forms. What would {{ formset.dish }} even refer to?
>> Only the forms inside the formset have a `dish` field.
>
> When I try {% for form in formset %}{{form.dish}}
>
> I get a "non iterable" error
The error doesn't apply to form.dish. Iterable errors refer to trying to
walk a variable that can't be walked, so the issue here is that formset
is not walkable. The code here irks me, cause you're using names that
may apply to django code. So just to be sure that's not the case, in
your view pass the DishesFormset() to the template as 'dishes_ordered'
or just 'borden' (using non-english language for variable names is a
good way to duck name clashes, allthough it makes it less readable for
people not familiar with the language).


> def testform(request):
> if request.method == 'POST':
> form = AddressForm(request.POST)
> formset = DishesFormset(request.POST)
> if formset.is_valid():
> return render_to_response('test', {
> 'form_data': formset.cleaned_data,
> })

So hmm, you're re-presenting the form on the same url? I guess for
debugging that works, but generally you'd redirect here.
--
Melvyn Sopacua


het.oosten

unread,
Jun 25, 2012, 8:08:35 AM6/25/12
to Django users
The answer to question 1:
***
1. in the template i can only get a form with {{formset}} .
{{formset.dish}} doesn't work
***
Was as easy as {{formset.form.dish}} (...sighs...)

>So hmm, you're re-presenting the form on the same url? I guess for
>debugging that works, but generally you'd redirect here.

This is only to get a basic prototype working. When it works i want to
redirect to another page with a confirmation.

When i enter in my form "Pancakes" and "Steak" this is the result:

[{}, {'dish': u'Steak'}]

het.oosten

unread,
Jun 25, 2012, 3:16:05 PM6/25/12
to Django users
This is how the POST looks:

name u'Johnson'
form-MAX_NUM_FORMS u' '
form-1-dish u'fried egg'
dish u'steak'
form-INITIAL_FORMS u'0'
csrfmiddlewaretoken u'c12349d127dbbd44e829e756613719c'
form-TOTAL_FORMS u'2'

het.oosten

unread,
Jun 26, 2012, 3:33:39 AM6/26/12
to django...@googlegroups.com
Ok when i render {{formset}} in the template everything works fine. When i render the form fields seperately, the first field of the "dishes form" is missing. This wont be hard to figure out further. Thanks for all the help!

Rob
Reply all
Reply to author
Forward
0 new messages