django表单向导as_view()示例因AttributeError而失败

时间:2013-02-21 06:15:25

标签: django django-forms

我是django的新手,我正在尝试使用表单向导。我正在关注official documentation,并从中尝试以下示例:

from django import forms
from django.contrib.formtools.wizard.views import SessionWizardView

class ContactForm1(forms.Form):
    subject = forms.CharField(max_length=100)
    sender = forms.EmailField()

class ContactForm2(forms.Form):
    message = forms.CharField(widget=forms.Textarea)

class ContactWizard(SessionWizardView):
    pass # I tried with a done() method as well, just to be sure.

initial = {
    '0': {'subject': 'Hello', 'sender': 'user@example.com'},
    '1': {'message': 'Hi there!'}
}
wiz = ContactWizard.as_view([ContactForm1, ContactForm2], initial_dict=initial)
form1 = wiz.get_form('0')
form2 = wiz.get_form('1')

但是上面的代码在form1 = wiz.get_form('0')行引发了以下错误:

Traceback (most recent call last):
  File "fw.py", line 19, in <module>
    form1 = wiz.get_form('0')
AttributeError: 'function' object has no attribute 'get_form'

这是文档中的错误,django本身,还是(很可能)我的代码有问题?我在Linux(Ubuntu)上使用django 1.4.1。

Google搜索仅显示两个相关链接:one from django IRC,另一个来自Django Brasil,但没有任何链接有任何解决方案。

1 个答案:

答案 0 :(得分:1)

我在this ticket向Django团队报告了此文档错误,现在已在master中修复。

应该是:

wiz = ContactWizard.as_view([ContactForm1, ContactForm2], initial_dict=initial)(request)