首页 > 其他 > 详细

WTForms 表单动态验证

时间:2014-10-25 17:14:45      阅读:543      评论:0      收藏:0      [点我收藏+]
class UserDetails(Form):
    group_id = SelectField(uGroup, coerce=int)

def edit_user(request, id):
    user = User.query.get(id)
    form = UserDetails(request.POST, obj=user)
    form.group_id.choices = [(g.id, g.name) for g in Group.query.order_by(name)]

choices是SelectField的内置属性

Note:

  Note we didn’t pass a choices to the SelectField constructor, but rather created the list in the view function. Also, the coerce keyword arg to SelectField says that we use int() to coerce form data. The default coerce is unicode().

值得注意的是:

  我们不需要把choices传递到SelectField构造器中,而是在view中直接创造一个嵌套list(key,value)。并且,关键字coerce表明我们把表单中的数据强制规定为int

 

WTForms 表单动态验证

原文:http://www.cnblogs.com/iwangzc/p/4050310.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!