一个简单的Form向导实现

Django的contrib中带了个FormWizard,但是我觉得它实现得很不好,不易使用,扩展性不强,所以就自己实现了一个。这里只说用法,首先得定义一个自己的Wizard,它继承SimpleWizard,并在构造函数中传递每一步需要的Form。

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,放在附件中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值