我想合并一个 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,我感觉更接近解决方案。非常感谢有关如何解决此错误的任何想法!感谢您的任何想法!