views:

330

answers:

2

I have an application where there is a FormWizard with 5 steps, one of them should only appear when some conditions are satisfied.

The form is for a payment wizard on a on-line cart, one of the steps should only show when there are promotions available for piking one, but when there are no promotions i want to skip that step instead of showing an empty list of promotions.

So I want to have 2 possible flows:

step1 - step2 - step3

step1 - step3
+1  A: 

The hook method process_step() gives you exactly that opportunity. After the form is validated you can modify the self.form_list variable, and delete the forms you don't need.

Needles to say if you logic is very complicated, you are better served creating separate views for each step/form, and forgoing the FormWizard altogether.

drozzy
A: 

I did it other way, overriding the render_template method. Here my solution. I didn't know about the process_step()...

def render_template(self, request, form, previous_fields, step, context):

    if not step == 0:
        # A workarround to find the type value!
        attr = 'name="0-type" value='
        attr_pos = previous_fields.find(attr) + len(attr)
        val = previous_fields[attr_pos:attr_pos+4]
        type = int(val.split('"')[1])

        if step == 2 and (not type == 1 and not type == 2 and not type == 3):
            form = self.get_form(step+1)
            return super(ProductWizard, self).render_template(request, form, previous_fields, step+1, context)

    return super(ProductWizard, self).render_template(request, form, previous_fields, step, context)