简体   繁体   English

WTForms/Flask:动态 min_entries

[英]WTForms/Flask: Dynamic min_entries

I'm looking to use the min_entries WTForms parameter dynamically ie without hardcoding numbers.我希望动态地使用 min_entries WTForms 参数,即没有硬编码数字。

It would look something like this in the form.py:它在 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')

and something like this in the views.py:在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():
        ...

This, however, does not work and simply renders NO FIELDS for the TestInputForm.但是,这不起作用并且只是为 TestInputForm 呈现 NO FIELDS。 If I put "min_entries = 10" into the students variable of the TestInputForm, everything works as expected.如果我将“min_entries = 10”放入TestInputForm 的students 变量中,一切都会按预期进行。 But I can't get it done dynamically.但我无法动态完成它。

Could anyone help me please?有人可以帮我吗? According to all my google/reddit/SO searches, this is basically the way most parameters or validators in WTForms are dynamically set.根据我所有的 google/reddit/SO 搜索,这基本上是 WTForms 中大多数参数或验证器的动态设置方式。

Thank you谢谢

I just found out about Field Lists and Form Fields, and scoured the internet for documentation or examples and found very little help.我刚刚发现了字段列表和表单字段,并在互联网上搜索了文档或示例,但几乎没有帮助。 I figured out what seems like a good way to get dynamic numbers of entries when you go to render your template, so I figured I'd submit it here in case it helps anyone.我想出了在您渲染模板时获取动态条目数量的好方法,所以我想我会在这里提交,以防它对任何人有帮助。 Note that this doesn't address adding entries on the fly using a button or anything like that.请注意,这并不能解决使用按钮或类似方法即时添加条目的问题。

def input(key_id):
    key = Testspecs.query.get_or_404(key_id) 
    student_number = key.student_number
    form = TestInputForm()
    # if the form was submitted, then it will collect all the entries for us
    if form.validate_on_submit():
        # form has whatever entries were just submitted
        for entry in form.students.entries:
            ...
        return(redirect(...))
    # if we get here, either validation failed or we're just loading the page
    # we can use append_entry to add up to the total number we want, if necessary
    for i in range(len(form.students.entries), student_number):
        form.students.append_entry()
    return render_template("input.html", form=form)

It isn't possible to override min_entries on a FieldList dynamically.动态覆盖min_entries上的FieldList是不可能的。

The workaround is to subclass the form and bind a new FieldList with the desired value.解决方法是对表单进行子类化并使用所需的值绑定一个新的FieldList

So your code would have to look something like this:所以你的代码必须看起来像这样:

def input(key_id):
    key = Testspecs.query.get_or_404(key_id) 
    student_number = key.student_number
    # Subclass form and bind new field
    class LocalForm(TestInputForm):pass
    LocalForm.students = FieldList(FormField(StudentForm), min_entries=student_number)
    # Use our new form
    form = LocalForm()
    if form.validate_on_submit():
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM