问题
Is possible that the steps of the wizard are dynamic? For example, the second step occur repeatedly n times?
回答1:
I had the same issue, and the form wizard (even in Django 1.4) just didn't work for me. It was so much customization that some things started to go wrong and debugging was awful.
I did write some code based on the existing clases. Please see my gists where I posted a solutions that worked great for me. If you have any comments or suggestions (including the name of the class), please post them.
Multi-page form manager, arranged as a (math) graph, with dynamic paths (next form depends on actual state and user input) and number of forms. Storage and validation are handled. Based in Django-1.4's
django.contrib.formtools.wizard.views.SessionWizardView
. https://gist.github.com/3098817Custom Django SessionStorage. Removed all the functionality that dealt with files. Based on Django-1.4's
django.contrib.formtools.wizard.storage.base.BaseStorage
anddjango.contrib.formtools.wizard.storage.session.SessionStorage
. https://gist.github.com/3080251
回答2:
What do you want to do ?
If you want to create a wizard where step x is repeated n times then answer is yes, you can do that and it is not that hard.
You just need to create a wizard class factory that creates the class given specific parameters and you're done.
In case you mean, can I change the steps of a wizard on-the-fly. answer is still yes but then things will get a bit more complicated than that since you will have to change the internal state of the wizard after its initialization.
This is not fun at all, if you really need the second option I really suggest to think about it, try to find an alternative design and choose the dynamic wizard approach as last resort.
回答3:
I struggled with this problem too. Tommaso Barbugli is right about creating a factory for the class. I'm 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)
来源:https://stackoverflow.com/questions/9777879/dynamic-number-of-steps-using-django-wizard