【发布时间】: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) 吗?此外,您似乎需要传递Stepobjects 而不是 djangoForm对象。 -
这给了我另一个错误:
TypeError at / All steps must be an instance of Step。这是 merlin 源代码第 42 行中的 located here。感谢您的任何想法! -
@mVChr:感谢您的想法。您能否提供一个小示例来说明如何传递 Step 对象而不是表单?这真的会帮助我理解如何解决这个问题
-
@NickB:你有使用 django-merlin 的工作代码吗?我想使用它,但是文档很差,没有示例。如果您有一些工作代码要显示,那将很有帮助
标签: django forms session wizard