I have one model and I’ve created a form out of the model using ModelForm. Now, I want to spread the form across two pages. For example, the first three fields will appear on the first page then the user clicks next and the last three fields appear on the second page. Then he clicks submit and the user submitted data is added to the database.
I took a look at the docs for the Form Wizard and it seems like it would work for model forms as well? Can someone confirm this?
And if it does, can someone explain the process of creating a WizardView class.
This example is given in the docs and I don’t understand what the second two parameters are. Is form_list just a list of form objects that you’ve instantiated based on your form classes? And what is **kwargs?
class ContactWizard(SessionWizardView):
def done(self, form_list, **kwargs):
do_something_with_the_form_data(form_list)
return HttpResponseRedirect('/page-to-redirect-to-when-done/')
Thanks in advance for your help!
Form Wizard is being built into Django 1.4 so is a good way to go about this. It should do what you want, but you may need a couple of tweaks.
Don’t worry about the
kwargsindone()at the moment – you’re not going to need them.form_listis the list of forms that you want to use for your steps – fromurls.py[ContactForm1, ContactForm2]will be passed todone()asform_list.What you will need to do is break your
ModelForminto separate forms. The easiest way to do this (if you want your model on several forms) is to not useModelFormbut just create your own form. It’s pretty easy:Once your forms reflect the portions of your model, just create the
viewsandpatternsas described in the docs and setdo_something_with_the_form_data(form_list)to a function that completes your model from the form data and then does a save.You could use
ModelFormbut – only if you can persuade it to produce different forms for Form Wizard to use for each step – that’s going to be the tricky part.