【发布时间】:2019-07-23 21:33:34
【问题描述】:
我希望动态使用 min_entries WTForms 参数,即不使用硬编码数字。
它在 form.py 中看起来像这样:
class TestSpecForm(FlaskForm):
student_number = IntegerField('Number of Students')
class StudentForm(FlaskForm):
answer = StringField('')
class TestInputForm(FlaskForm):
students = FieldList(FormField(StudentForm)) # I'd like to insert the dynamic min_entries here
submit = SubmitField('Submit')
以及views.py中的类似内容:
def input(key_id):
key = Testspecs.query.get_or_404(key_id)
student_number = key.student_number
form = TestInputForm()
form.students.min_entries = student_number
if form.validate_on_submit():
...
但是,这不起作用,只会为 TestInputForm 呈现 NO FIELDS。如果我将“min_entries = 10”放入 TestInputForm 的学生变量中,一切都会按预期工作。但我无法动态完成它。
谁能帮帮我?根据我所有的 google/reddit/SO 搜索,这基本上是 WTForms 中大多数参数或验证器的动态设置方式。
谢谢
【问题讨论】:
标签: python flask dynamic parameters wtforms