9

在 Django Wizard 的文档中,我发现了如下代码:

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

所以我想知道如何将多个表单添加到向导的单个步骤

4

3 回答 3

3

使您的一个表格Formset包含您需要的其余表格。您不一定需要使用 a ModelFormset,您可以继承基类并手动创建表单。

于 2013-10-23T11:17:33.993 回答
1

现在不推荐使用此链接:https ://github.com/vikingco/django-formtools-addons

如果对任何人有帮助,我想分享我的设置:

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],

        })
于 2017-02-05T18:08:34.830 回答
0

我已经实现了 Django 向导的扩展,它在一个向导步骤中支持多个表单:

https://pypi.python.org/pypi/django-multipleformwizard

于 2015-03-02T10:13:47.373 回答