1

我想合并一个 FormWizard 来处理长表格。经过研究,似乎django-merlin是最好的选择,因为它通过会话管理表单向导。但是,尝试合并它(如django 向导文档中所述)会导致AttributeError: type object 'CreateWizard' has no attribute 'as_view'.

这是它的样子:

from merlin.wizards.session import SessionWizard

class StepOneForm(forms.Form):
    year = forms.ChoiceField(choices=YEAR_CHOICES)
    ...

class StepTwoForm(forms.Form):
    main_image = forms.ImageField()
    ...

class StepThreeForm(forms.Form):
    condition = forms.ChoiceField(choices=CONDITION)
    ...

class CreateWizard(SessionWizard):
    def done(self, form_list, **kwargs):
        return HttpResponseRedirect(reverse('wizard-done'))

网址:

url(r'^wizard/(?P<slug>[A-Za-z0-9_-]+)/$', CreateWizard.as_view([StepOneForm, StepTwoForm, StepThreeForm])),

由于 merlin 文档有点稀疏,我选择使用as_view()原始 django 表单向导文档中描述的方法,但结果是AttributeError. 我应该如何在我的 urlconf 中加入 merlin 向导?谢谢你的想法!


这是我根据@mVChr 的回答更新后得到的错误和回溯,并定义如下步骤:

step_one = Step('step_one', StepOneForm())

错误和回溯:

TypeError at / issubclass() arg 1 must be a class

Traceback:
File /lib/python2.7/django/core/handlers/base.py" in get_response
  89.                     response = middleware_method(request)
File "/lib/python2.7/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/myproject/myproject/urls.py" in <module>
  7. from myapp.forms import step_one, step_two, step_three, CreateWizard
File "/myproject/myapp/forms.py" in <module>
  16. step_one = Step('step_one', StepOneForm())
File "/lib/python2.7/merlin/wizards/utils.py" in __init__
  36.         if not issubclass(form, (forms.Form, forms.ModelForm,)):

Exception Type: TypeError at /
Exception Value: issubclass() arg 1 must be a class

尽管我仍然遇到错误,但感谢@mVChr,我感觉更接近解决方案。非常感谢有关如何解决此错误的任何想法!感谢您的任何想法!

4

2 回答 2

0

注意:我不知道这是否可行,我只是想帮助 Nick B 用一个具体的例子翻译文档,让他更接近正确的解决方案。请让我知道这是否按原样工作,我将删除此评论。

从阅读文档看来,您需要将Step对象列表直接传递给您的SessionWizard子类实例化,如下所示:

from merlin.wizards.session import SessionWizard
from merlin.wizards.utils import Step

class StepOneForm(forms.Form):
    year = forms.ChoiceField(choices=YEAR_CHOICES)
    ...
step_one = Step('step-one', StepOneForm())

class StepTwoForm(forms.Form):
    main_image = forms.ImageField()
    ...
step_two = Step('step-two', StepTwoForm())

class StepThreeForm(forms.Form):
    condition = forms.ChoiceField(choices=CONDITION)
    ...
step_three = Step('step-three', StepThreeForm())

class CreateWizard(SessionWizard):
    def done(self, form_list, **kwargs):
        return HttpResponseRedirect(reverse('wizard-done'))

然后在你的urls.py

url(r'^wizard/(?P<slug>[A-Za-z0-9_-]+)/$',
    CreateWizard([step_one, step_two, step_three]))
于 2013-02-04T19:53:43.367 回答
0

想要引起您的注意,您在创建 Step 的对象时使用了错误的语法。这个

step_one = Step('step-one', StepOneForm())

应该像

step_one = Step('step-one', StepOneForm)

您必须在所有步骤对象中更正它。

于 2015-11-18T08:29:35.010 回答