在WTForms字段上设置数据属性 [英] Setting data attributes on a WTForms field

查看:150
本文介绍了在WTForms字段上设置数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将data-属性添加到表单字段以与Bootstrap集成。我在模板中尝试了以下内容:

$ $ p $ $ $ $ c $ {form.test(data-toggle =toggle,data-size = mini,data-on =是,data-off =No,type =checkbox)}}

得到这个错误:

$ p $ TemplateSyntaxError:expected token',',got'='

为什么我得到这个错误,我该如何解决?


I want to add "data-" attributes to a form field for integration with Bootstrap. I tried the following in a template:

{{ form.test(data-toggle="toggle", data-size="mini", data-on="Yes", data-off="No", type="checkbox")}}

and got this error:

TemplateSyntaxError: expected token ',', got '='

Why did I get this error and how do I fix it?

解决方案

You need to use valid Python names as the variable names. Therefore names like "data-toggle" are invalid because they have a "-" in them. Change the names to use underscores, like "data_toggle". WTForms automatically converts "_" to "-" for keywords it doesn't recognize.

这篇关于在WTForms字段上设置数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆