Django FormWizard as_view()方法AttributeError

时间:2013-02-04 19:13:35

标签: django forms session wizard

我想要使用FormWizard来处理长格式。经过研究,似乎django-merlin是最好的选择,因为它通过会话来管理formwizard。但是,尝试合并它(如django wizard docs中所述)会产生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:

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

由于merlin文档有点稀疏,我选择使用原始django表单向导文档中描述的as_view()方法,但结果是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让我更接近解决方案。任何有关如何解决此错误的想法都非常感谢!谢谢你的任何想法!

2 个答案:

答案 0 :(得分:0)

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

从阅读the docs看起来您需要将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]))

答案 1 :(得分:0)

想要引起注意,在制作Step的对象时使用了错误的语法。 此

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

应该像

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

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