Django formwizard with formsets and forms on same page

蹲街弑〆低调 提交于 2019-12-24 05:19:10

问题


I'm working with Django 1.4 FormWizard (specifically, NamedUrlFormWizard)

first, the basics. i have a 3 step form wizard i'm building. The final outcome is along the lines of defining a template, and then choosing some people to use it, and then send them an email.

  • Step 1 - enter in basic template data (name, description, etc)
  • Step 2 - define a list of N fields, each with their own set of attributes but all identical in structure
  • Step 3 - choose one or more users to email, AND customize the contents of this email before saving

so far, in the form wizard:

  • Step 1 is a standard Form class, and works just fine.
  • Step 2 is a standard Formset class, and works just fine.
  • Step 3 is giving me some trouble. It needs to be a formset (list of email addresses), but also an additional form input field with email text. I can't figure out how to have both a formset in addition to a non-repeating form input on the same page inside of a form wizard.

in a perfect world, i could define a Formset as just another form field in a Form definition. ie:

class EmailAddressForm(forms.Form):
    email = forms.EmailField()

class EmailAddressesAndText(forms.Form):
    emailText = forms.Textarea()
    emailAddressFormSet = formset_factory(EmailAddressForm, etc etc)

then point my FormWizard page at 'EmailAddressesAndText' and be done with it. but its not a perfect world. Any ideas on how I can achieve such a thing?


回答1:


You can use get_form() method of WizardView to customize the form for particular step. Refer: WizardView.get_form. This answer can help you to add a field

Another option would be to add the field in formset and through JS disable and hide all instances other than first one.



来源:https://stackoverflow.com/questions/12065743/django-formwizard-with-formsets-and-forms-on-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!