Django formwizard与同一页上的formets和表单 [英] Django formwizard with formsets and forms on same page

查看:226
本文介绍了Django formwizard与同一页上的formets和表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.4 FormWizard(具体来说,NamedUrlFormWizard)



首先是基础知识。我有一个3步形式向导我正在建设。最终的结果是定义一个模板,然后选择一些人使用它,然后发送他们一封电子邮件。




  • 步骤1 - 输入基本模板数据(名称,描述等)

  • 步骤2 - 定义N个字段的列表,每个字段都有自己的一组属性,但在结构上都是相同的。 li>
  • 步骤3 - 选择一个或多个用户进行电子邮件,并在保存之前自定义此电子邮件的内容



到目前为止,在表单向导中:




  • 第1步是一个标准的Form类,工作正常。

  • 步骤2是一个标准的Formset类,工作正常。

  • 步骤3给我一些麻烦。它需要一个表单集(电子邮件地址列表),还有一个带电子邮件文本的附加表单输入字段。我不知道如何在表单向导中的同一页面上添加一个不重复的表单输入的方式。


$ b $在完美的世界中,我可以将Formset定义为Form定义中的另一个表单域。
ie:

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

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

然后将我的FormWizard页面指向EmailAddressesAndText,并使用它完成。但它不是一个完美的世界。任何有关如何实现这样的事情的想法?

解决方案

可以使用 get_form() code>方法 WizardView 来定制特定步骤的窗体。参考: WizardView.get_form 此答案可以帮助您添加字段



另一个选择是在formset中添加字段,通过JS禁用并隐藏除第一个以外的所有实例。


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?

解决方案

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.

这篇关于Django formwizard与同一页上的formets和表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆