Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django FormWizard as_view()方法AttributeError_Django_Forms_Session_Wizard - Fatal编程技术网

Django FormWizard as_view()方法AttributeError

Django FormWizard as_view()方法AttributeError,django,forms,session,wizard,Django,Forms,Session,Wizard,我想合并一个FormWizard来处理长表单。经过研究,这似乎是最好的选择,因为它通过会话管理formwizard。但是,如中所述尝试合并它会导致AttributeError:type对象“CreateWizard”没有属性“as_view” 下面是它的外观: from merlin.wizards.session import SessionWizard class StepOneForm(forms.Form): year = forms.ChoiceField(choices=Y

我想合并一个FormWizard来处理长表单。经过研究,这似乎是最好的选择,因为它通过会话管理formwizard。但是,如中所述尝试合并它会导致AttributeError:type对象“CreateWizard”没有属性“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'))
网址:

错误和回溯:

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,我感觉离解决方案更近了。任何关于如何解决此错误的想法都将不胜感激!谢谢你的建议

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

通过阅读,您似乎需要将步骤对象列表直接传递给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'))
然后在URL.py中:


希望引起您的注意,您在创建步骤对象时使用了错误的语法。 这个

应该是

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

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

您是否尝试过创建向导[StepOneForm,StepTwoForm,StepThreeForm]?另外,似乎您需要传递而不是django表单对象,这给了我另一个错误:TypeError at/All steps必须是Step的实例。这是梅林资料来源,第42行。谢谢你的建议@mVChr:谢谢你的想法。您能否提供一个小示例来说明如何传递Step对象而不是表单?这将真正帮助我理解如何解决这个问题issue@NickB:您是否有使用django merlin的工作代码。我想使用这个,但是文档很差,没有示例。如果您有一些工作代码要显示,那将非常有用。非常感谢您的想法@mVChr!按照你的建议,我想我更接近了,但我现在有一个新的错误消息:TypeError at/issubclass arg 1必须是一个类。此外,表单必须是Django表单的子类。查看调试屏幕中的局部变量也会告诉我:self:格式化错误:“Step”对象没有属性“slug”。我不知道如何继续解决这个错误,但非常感谢任何智慧的花絮!谢谢
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'))
url(r'^wizard/(?P<slug>[A-Za-z0-9_-]+)/$',
    CreateWizard([step_one, step_two, step_three]))
step_one = Step('step-one', StepOneForm())
step_one = Step('step-one', StepOneForm)