Skip steps on django FormWizard

I have an application in which there is FormWizard with 5 steps, one of them should appear only if certain conditions are met.

A form for the payment wizard in an online cart, one of the steps should only show when there are promotions available for picking, but when there are no promotions, I want to skip this step, instead of showing an empty list of promotions.

So, I want to have 2 possible threads:

step1 - step2 - step3

step1 - step3
+3
source share
3 answers

hook process_step() ​​. self.form_list , .

, , , / FormWizard.

+6

, , FormView, urls.py:

contact_forms = [ContactForm1, ContactForm2]

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

. Django: https://django-formtools.readthedocs.io/en/latest/wizard.html#conditionally-view-skip-specific-steps

+4

, render_template. . 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)
+1

Source: https://habr.com/ru/post/1711657/


All Articles