views:

269

answers:

1

Hi guys, i have 2 day's thinking how make this.

I have two forms (really 4), the first form have radio buttons, where the second form will be different if the user choice x radio button option.

Like alway's sorry with my English.

ill explain :

First Form have an options with radio buttons, with the options ill bring the "called form", i have 3 form's waiting for.

I was reading about WizardForm, but i don't know how bring the second form in dynamic way.

Please a need some help with this :(

Thanks

A: 

It will probably help if you read the contrib/formtools/wizard.py code. It's fairly straightforward and following it will give you a better sense of what you need to do.

There is a process_step hook in there which you'll need to override in your own form wizard. What I did is look at what step I was at do some special processing if the form validated (ie, it had cleaned_data):

class MyFormWizard(FormWizard):

    def process_step(self, request, form, step):
        if step == 0 and hasattr(form, 'cleaned_data'):
            # Do special stuff

If I understand your question correctly, you want the 2nd step to serve a particular form based on the user's choice from the 1st form. In that case, what I might try first is to build a dynamic generic form for the 2nd step, where the fields used are based on the results from the first form (untested):

    def process_step(self, request, form, step):
        if step == 0 and hasattr(form, 'cleaned_data'):
            # The initial attribute is a dictionary which maps the step number
            # to a dictionary of what should be initial values.
            # You can use/abuse this in a form's constructor
            self.initial[1] = {'fields': {'field1': field, 'field2': another}}

Then, in your 2nd form

class MySecondForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(StatBuilderForm2, self).__init__(*args, **kwargs)

        for name, field in kwargs['initial']['fields'].iteritems():
              self.fields[name] = field
brianz