【问题标题】:Django FormWizard as_view() method AttributeErrorDjango FormWizard as_view() 方法 AttributeError
【发布时间】:2013-01-19 13:40:44
【问题描述】:

我想合并一个 FormWizard 来处理长表单。经过研究,似乎django-merlin 是最好的选择,因为它通过会话管理表单向导。但是,尝试合并它(如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(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,我感觉离解决方案更近了。非常感谢有关如何解决此错误的任何想法!感谢您的任何想法!

【问题讨论】:

  • 你试过CreateWizard([StepOneForm, StepTwoForm, StepThreeForm]) (see docs) 吗?此外,您似乎需要传递 Step objects 而不是 django Form 对象。
  • 这给了我另一个错误:TypeError at / All steps must be an instance of Step。这是 merlin 源代码第 42 行中的 located here。感谢您的任何想法!
  • @mVChr:感谢您的想法。您能否提供一个小示例来说明如何传递 Step 对象而不是表单?这真的会帮助我理解如何解决这个问题
  • @NickB:你有使用 django-merlin 的工作代码吗?我想使用它,但是文档很差,没有示例。如果您有一些工作代码要显示,那将很有帮助

标签: django forms session wizard


【解决方案1】:

注意:我不知道这是否可行,我只是想通过一个具体的例子帮助 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]))

【讨论】:

  • 非常感谢您的想法@mVChr!听从了你的建议,我想我更接近了,但我现在有一个新的错误消息:TypeError at /issubclass() arg 1 must be a class. 另外,Form must be a subclass of a Django Form。查看调试屏幕中的局部变量也告诉我:self: Error in formatting: 'Step' object has no attribute 'slug'。我不知道如何着手解决这个错误,但非常感谢任何智慧的花絮!谢谢!
【解决方案2】:

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

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

应该是这样的

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

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

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 2015-10-08
    • 2013-01-25
    • 2012-05-22
    • 2014-09-03
    • 2015-06-25
    • 2013-10-23
    • 2013-10-14
    相关资源
    最近更新 更多