我在使用WTForms创建动态RadioField时遇到了问题...
当我尝试基本示例时:
targeting_type = RadioField('Label', choices=[('value', 'description'),
('value_two', 'whatever')])一切都很好。
当我尝试使用这个例子时:Flask-SQLAlchemy wtform based on db
在表单中是来自DB的值。但是当我点击Submit按钮时,页面被“重新加载”,但可能是“没有数据”。
我的views.py示例:
form = TargetingTypeForm()
form.targeting_type.choices = [
(targeting_type.id, targeting_type.name)
for targeting_type in SettingsTargetingType.query.all()]
if form.validate_on_submit():
print('test', form.targeting_type.data)提交此表单后,测试数据未打印:/
请用WTForms + SQLAlchemy查询创建RadioField的正确方法是什么?
谢谢你的回答。
发布于 2018-09-06 17:52:53
由于您使用ID作为值,并且我猜它是一个整数,所以您必须在RadioField上使用coerse属性!
试试这个:
form = TargetingTypeForm()
form.targeting_type.choices = [
(targeting_type.id, targeting_type.name)
for targeting_type in SettingsTargetingType.query.all()]
form.targeting_type.coerse = int
if form.validate_on_submit():
print('test', form.targeting_type.data)或者将coerse=int添加到TargetingTypeForm类中的targeting_type定义
https://stackoverflow.com/questions/52199558
复制相似问题