Dynamic step count using Django wizard

Is it possible that the steps of the wizard are dynamic? For example, is the second step repeated several times?

+3


source to share


3 answers


I had the same problem and the form wizard (even in Django 1.4) just didn't work for me. There were so many tweaks that things started to go wrong and the debugging was awful.

I wrote a code based on existing clans. Please see my gists where I posted solutions that worked great for me. If you have any comments or suggestions (including the class name), please post them.



  • Multi-page form manager, organized as a (math) graph, with dynamic paths (next form depends on actual state and user input) and number of forms. Storage and verification are performed. Based in Django-1.4 django.contrib.formtools.wizard.views.SessionWizardView

    . https://gist.github.com/3098817

  • Custom SessionStorage for Django. Removed all functions related to files. Based on Django-1.4 django.contrib.formtools.wizard.storage.base.BaseStorage

    and django.contrib.formtools.wizard.storage.session.SessionStorage

    . https://gist.github.com/3080251

+4


source


What do you want to do?

If you want to create a wizard that repeats step x n then the answer is yes, you can do it and it is not that hard.

You just need to create a master factory class that creates a class with given specific parameters and you're done.



In case you mean, I can change the wizard's actions on the fly. the answer is still yes, but then things get a little more complicated than that because you will have to change the internal state of the master after initializing it.

It's not fun, if you really want the second option, I really suggest thinking about it, try to find an alternative design and take the dynamic wizard approach as a last resort.

+2


source


I was struggling with this problem too. Tommaso Barbugli is right about creating a factory for the class. I am currently working with Django 1.6.

in the url, include this:

url('/create_wizard/', factory_wizard, name='factory_wizard')

      

this is the factory:

class WizardClass(SessionWizardView):
    ...

def factory_wizard(request, *args, **kwargs):
    parameter_to_know_which_step_number = #  I let you implement this one ( I did it by the session data )
    ret_form_list = [FirstFormClass, SecondFormClass]

    for _ in range(parameter_to_know...):
        form_list.append(SecondFormClass)

    class ReturnClass(WizardClass):
        form_list = ret_form_list

    return ReturnClass.as_view()(request, *args, **kwargs)

      

+1


source







All Articles