Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip steps on a django FormWizard

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
like image 403
hchinchilla Avatar asked Dec 18 '22 07:12

hchinchilla


2 Answers

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.

like image 165
Andriy Drozdyuk Avatar answered Dec 19 '22 19:12

Andriy Drozdyuk


To make certain forms optional you can introduce conditionals in the list of forms you pass to the FormView in your urls.py:

contact_forms = [ContactForm1, ContactForm2]

urlpatterns = patterns('',
    (r'^contact/$', ContactWizard.as_view(contact_forms,
        condition_dict={'1': show_message_form_condition}
    )),
)

For a full example see the Django docs: https://django-formtools.readthedocs.io/en/latest/wizard.html#conditionally-view-skip-specific-steps

like image 39
tijs Avatar answered Dec 19 '22 21:12

tijs