Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Wizard, multiple forms in one step

In documentation of Django Wizard i found code like this:

{{ wizard.management_form }}
{% if wizard.form.forms %}
    {{ wizard.form.management_form }}
    {% for form in wizard.form.forms %}
        {{ form }}
    {% endfor %}
{% else %}
    {{ wizard.form }}
{% endif %}

So I am wondering how can i add multiple forms to single step of wizard

like image 652
mdargacz Avatar asked Jun 27 '13 13:06

mdargacz


2 Answers

Make one of your forms a Formset containing the rest of the forms you need. You don't need to necessarily use a ModelFormset, you can subclass the base class and create the forms manually.

like image 166
patrys Avatar answered Oct 14 '22 09:10

patrys


This is now deprecated use this link: https://github.com/vikingco/django-formtools-addons

I wanted to share my settings if would be any help to anyone:

class BaseImageFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(BaseImageFormSet, self).__init__(*args, **kwargs)
        self.queryset = Images.objects.none()


ImageFormSets = modelformset_factory(Images, formset=BaseImageFormSet, fields=('picture',), extra=2)

form_list = [("step1", CategoryForm),
         ("step2", CityForm),
         ("step3", (
            ('lastform', LastForm),
            ('imageform', ImageFormSets)
          ))
         ]

templates = {"step1": "create_post_category.html",
             "step2": "create_post_city.html",
             "step3": "create_post_final.html"}




class OrderWizard(SessionMultipleFormWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'photos')) 


    def get_template_names(self):
        return [templates[self.steps.current]]


    def render(self, forms=None, **kwargs):
        forms = forms or self.get_forms()
        context = self.get_context_data(forms=forms, **kwargs)

        #print(forms[1](queryset = Images.objects.none()))
        return self.render_to_response(context)

    def done(self, form_list, form_dict, **kwargs):
        form_data_dict = self.get_all_cleaned_data()
        #print(form_data_dict)
        result = {}
        instance = Post()
        #print(form_dict)
        for key in form_dict:
            form_collection = form_dict[key]
            #print(form_collection)
            for key in form_collection:
                form = form_collection[key]
                print('printing form %s' % key)
                #if isinstance(form, forms.ModelForm):
                if key == 'lastform':
                    post_instance = form.save(commit=False)

                    nodes = form_data_dict.pop('nodes')
                    city = form_data_dict.pop('city')
                    post_instance.save()

                    post_instance.category.add(nodes)
                    post_instance.location.add(city)
                    print('lastfome as esu ')

                if key == 'imageform':
                    for i in form_data_dict['formset-step3']:
                        picture = i.pop('picture')
                        images_instance = Images(post=post_instance, picture=picture)
                        images_instance.save()







        return render_to_response('create_post_done.html', {
            'form_data': result,
            #'form_list': [form.cleaned_data for form in form_list],

        })
like image 35
Otas Ilciukas Avatar answered Oct 14 '22 08:10

Otas Ilciukas