Django的contrib中带了个FormWizard,但是我觉得它实现得很不好,不易使用,扩展性不强,所以就自己实现了一个。这里只说用法,首先得定义一个自己的Wizard,它继承SimpleWizard,并在构造函数中传递每一步需要的Form。
上面定义的Form1,Form2和Form3就是普通的Form,继承于普通的forms.Form。step1, step2, step3则是用来标识每一步的名称,不像contrib中的FormWizard,我没有使用数字来标识每一步。实际上,你可以使用任何对象来标识每一步,包括数字,但是使用字符串就足够了。
然后需要定义TestWizard的render和done方法:
render方法返回一个包含两个元素的tuple,第一个用表示用来渲染每一步的template名称,第二个是传递给template的context,它是一个dict对象。默认传递给模板的有两个对象,一个WizardStatus对象,可以通过wizard_status来引用它,它包含step和data两个属性。step标识当前的步骤,data表示之前所有填过的数据,这是一个dict对象,里面的数据都是cleaned_data,这表示如果你在Form中定义了一个IntField,那么data对那个字段返回的数据将是int类型,而不是string。
done方法则是wizard最后一步所做的事情,它通常是会将所有的数据保存数据库,然后重定向到一个新的数据展示页面。这里的例子只是显示wizard_done.html页面,并传递wizard_status对象。
下面来看看模板的写法:
wizard_step1.html
最关键的是必须要包含{{ wizard_status.as_hidden }},因为我使用隐藏字段来保存上次填充数据。类似的完成wizard_step2.html,wizard_step3.html。
最后别忘了添加test_wizard到urls.py中:
urls.py:
附上TestWizard的完整代码:
views.py:
最后是wizard.py,放在附件中。
class TestWizard(wizard.SimpleWizard):
def __init__(self):
super(TestWizard, self).__init__(
(
('step1', Form1),
('step2', Form2),
('step3', Form3),
)
)
test_wizard = TestWizard()
上面定义的Form1,Form2和Form3就是普通的Form,继承于普通的forms.Form。step1, step2, step3则是用来标识每一步的名称,不像contrib中的FormWizard,我没有使用数字来标识每一步。实际上,你可以使用任何对象来标识每一步,包括数字,但是使用字符串就足够了。
然后需要定义TestWizard的render和done方法:
class TestWizard(wizard.SimpleWizard):
# return (template_name, context) tuple
def render(self, step, **kwargs):
return ('wizard_%s.html' % step, None)
def done(self, wizard_status, request, **kwargs):
return render_to_response('wizard_done.html', {
'wizard_status': wizard_status,
}, context_instance=RequestContext(request)
)
render方法返回一个包含两个元素的tuple,第一个用表示用来渲染每一步的template名称,第二个是传递给template的context,它是一个dict对象。默认传递给模板的有两个对象,一个WizardStatus对象,可以通过wizard_status来引用它,它包含step和data两个属性。step标识当前的步骤,data表示之前所有填过的数据,这是一个dict对象,里面的数据都是cleaned_data,这表示如果你在Form中定义了一个IntField,那么data对那个字段返回的数据将是int类型,而不是string。
done方法则是wizard最后一步所做的事情,它通常是会将所有的数据保存数据库,然后重定向到一个新的数据展示页面。这里的例子只是显示wizard_done.html页面,并传递wizard_status对象。
下面来看看模板的写法:
wizard_step1.html
<form action="." method="POST">
<h2> {{ wizard_status.step }}</h2>
{{ form.as_p }}
{{ wizard_status.as_hidden }}
<input type="submit" value="Continue" />
</form>
最关键的是必须要包含{{ wizard_status.as_hidden }},因为我使用隐藏字段来保存上次填充数据。类似的完成wizard_step2.html,wizard_step3.html。
最后别忘了添加test_wizard到urls.py中:
urls.py:
urlpatterns = patterns('',
(r'^test_wizard/$', views.test_wizard),
)
附上TestWizard的完整代码:
views.py:
class TestWizard(wizard.SimpleWizard):
class Form1(forms.Form):
field1 = forms.CharField(max_length = 20)
class Form2(forms.Form):
field2 = forms.CharField(max_length = 20)
class Form3(forms.Form):
field3 = forms.CharField(max_length = 20)
def __init__(self):
super(TestWizard, self).__init__(
(
('step1', self.Form1),
('step2', self.Form2),
('step3', self.Form3),
)
)
# return (template_name, context) tuple
def render(self, step, **kwargs):
return ('wizard_%s.html' % step, None)
def done(self, wizard_status, request, **kwargs):
return render_to_response('wizard_done.html', {
'wizard_status': wizard_status,
}, context_instance=RequestContext(request)
)
test_wizard = TestWizard()
最后是wizard.py,放在附件中。