2013-06-27 36 views
7

在Django的嚮導的文檔,我發現這樣的代碼:Django的嚮導,一步到位多種形式

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

所以我想知道我怎麼可以將多個表單添加到嚮導的單步

+0

在這個問題上花了幾個小時後,我終於找到了一個似乎是解決方案:https://code.djangoproject.com/ticket/18830但它不是一種滿足我的方式,因爲我無法使用循環來發布表單字段。任何其他想法? – mdargacz

+1

循環用於formset。爲什麼你需要在單步中添加多個表單? – mariodev

+0

也許你正在尋找formset https://docs.djangoproject.com/en/1.5/topics/forms/formsets/#django.forms.formsets.BaseFormSet – SaulTigh

回答

3

製作一個包含您需要的其他表格的Formset。您無需使用ModelFormset,您可以繼承基類並手動創建表單。

+0

沒有批准的答案呢? – Joe

+0

這隻適用於您使用同一表單的多個實例,否? – Pieter

+0

不,它適用於任何形式的組合。 – patrys

0

這是現在不贊成使用此鏈接: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], 

     })