Django FormWizard as_view()方法AttributeError [英] Django FormWizard as_view() method AttributeError

查看:162
本文介绍了Django FormWizard as_view()方法AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入一个FormWizard来处理一个很长的表单。经过研究,似乎 django-merlin 是最好的选择,因为它通过会话管理formwizard。试图整合它(如 django向导文档),但是会导致一个 AttributeError:type对象'CreateWizard'没有属性'as_view'

I want to incorporate a FormWizard to handle a long form. After researching, it seems that django-merlin is the best option since it manages the formwizard via sessions. Trying to incorporate it (as mentioned in the django wizard docs), however, results in an 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文档有点稀疏,所以我选择使用 as_view()方法,如原始django表单向导文档中所述,但它会导致 A ttributeError 。我应该如何在我的urlconf中加入merlin向导?感谢您的想法!

Since the merlin docs are a little sparse, I chose to use the as_view() method as described in the original django form wizard docs, but it results in an AttributeError. How should I incorporate the merlin wizard in my urlconf? Thanks for your ideas!

这是基于@ mVChr的答案和定义步骤后更新的错误和追溯像这样:

This is the error and traceback that I get after updating based on @mVChr's answer and defining steps like this:

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的解决方案。任何想法如何解决这个错误非常感谢!感谢任何想法!

Although Im still getting an error, I feel closer to the solution thanks to @mVChr. Any ideas on how to solve this error are greatly appreciated! Thanks for any ideas!

推荐答案

想要提请您注意,在制作Step的对象时使用错误的语法。
这个

Want to draw your attention that you use wrong syntax while making an object of Step. This

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

应该像

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

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

You have to correct it in all step object.

这篇关于Django FormWizard as_view()方法AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆