【发布时间】:2020-04-22 09:26:04
【问题描述】:
我的 Flask 应用程序中的 validate_on_submit() 方法存在以下问题。
快速概览我所拥有的:
我的表单类是根据输入动态创建的:
class EditUsers(FlaskForm):
submit = SubmitField('Submit')
def form_builder(roles_list):
class EditUsersForm(EditUsers):
pass
for role in roles_list:
if isinstance(role, tuple) and len(role) == 5:
if not role[2] and role[3]:
setattr(EditUsersForm, f'{role[0]}_{role[4]}_bool', BooleanField(label=role[0]))
setattr(EditUsersForm, f'{role[0]}_{role[4]}_date', SelectField('Expiration Period', choices=choices))
else:
raise IncorrectType
IncorrectType 是我准备的自定义异常,并且选择是使用同一文件中的日期时间创建的(这无关紧要,因此我没有将其包含在代码中)。
我在烧瓶应用程序中的路线(简化):
#### EDIT: I pasted the wrong route, POST and GET Methods are included###
@edit_users.route('/users', methods=['GET', 'POST'])
def users():
... # Some code there
form = form_builder(roles_to_form)
print(form.is_submitted())
print(form.validate())
print(form.validate_on_submit())
return render_template('edit_user.html',
data_dict=data_dict, # in data_dict i pass form fields names and some other data
form=form,
)
我的模板:
<!-- Some usual stuff goes there css js etc -->
<div >
<form class="form form-horizontal" method="post" role="form" style="margin: auto; text-align: center; width: 40%;">
{{ form.csrf_token() }}
<table class="table" align="center">
<thead>
<th>Role</th>
<th>Expiration Date</th>
<th>Give Role?</th>
</thead>
{% for field in data_dict['id_list'] %}
<tr>
<td align="left">{{ field[2] }}</td>
<td align="left">
{{ form[field[1]] }}
</td>
<td align="left">{{ form[field[0]] }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="3" align="center">{{ form.submit() }}</td>
</tr>
</table>
</form>
</div>
我的问题是什么?
当我点击提交按钮时,is_submitted() 返回 True 但 validate() 不返回,因此我不能使用典型的 if form.validated_on_submit(),因为即使我提交表单也会返回 False。
我挖了一点,发现了一些不寻常的东西。我使用受保护的表单属性成员来检查 validate() 函数视为输入的任何内容:
for name in form._fields:
print(name)
inline = getattr(form.__class__, 'validate_%s' % name, None)
print(inline)
输出(不要介意字段名称):
提交
无
Engineer_2_bool
无
Engineer_2_date
无
Operator_3_bool
无
Operator_3_date
无
Supervisor_4_bool
无
Supervisor_4_date
无
Guest_5_bool
无
Guest_5_date
无
csrf_token
无
我不知道为什么我的表单没有validate_{name} 属性。
【问题讨论】:
-
这里没有足够的信息可以关闭。
choices是字符串列表还是元组列表?在致电form.validate()后,您是否检查过form.errors的输出? (只需在print(form.validate())之后执行print(form.errors)) -
@tresni 我最近检查了错误,确实问题出在
choices。我忘记将其类型从 datetime.datetime 更改为 str。感谢您的输入
标签: python-3.x flask jinja2 flask-wtforms wtforms