码迷,mamicode.com
首页 > 其他好文 > 详细

WTForms 表单动态验证

时间:2014-10-25 17:17:02      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

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 表单动态验证

标签:style   blog   http   color   io   os   ar   for   sp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!