简体   繁体   English

Flask WTForms 和使用 for 循环生成字段

[英]Flask WTForms and generating fields with for loop

I want to create a form with fields for each of the last 10 years.我想为过去 10 年的每一年创建一个包含字段的表单。 So for 2020-2011 I want, say, an IntegerField for each year with the variable names year_2020, year_2019, year_ 2018, ... It'd also be nice if they had the appropriate labels too, ie 2020, 2019...因此,对于 2020-2011 年,我希望每年都有一个 IntegerField,变量名称为 year_2020、year_2019、year_2018、...如果它们也有适当的标签,例如 2020、2019...

I could do this by writing something out for each year individually but I thought it'd be nicer and more efficient (?) to generate them using a for loop.我可以通过为每年单独写一些东西来做到这一点,但我认为使用 for 循环生成它们会更好、更有效(?)。 Is this possible?这可能吗?

I saw this question about generating fields in the template using a for loop but I'm wondering how to generate fields in the python form class itself (not sure what to call it; please excuse my ignorance).我看到了这个关于使用 for 循环在模板中生成字段的问题,但我想知道如何在 python 表单中生成字段 class 本身(不知道该怎么称呼它;请原谅我的无知)。 I've seen this question but I can't seem to get it to work.我已经看到了这个问题,但我似乎无法让它发挥作用。 I'm also not fond of that solution since it doesn't give the fields descriptive names like year_2020.我也不喜欢该解决方案,因为它没有为字段提供描述性名称,例如 year_2020。

This is my code so far;到目前为止,这是我的代码; apologies for any errors.为任何错误道歉。

The python: python:

forms.py
    class YearForm(FlaskForm):
        year = IntegerField(validators=[NumberRange(min=0,max=100000,message='Please enter an integer above 0.'),
                            InputRequired(message='Please enter a value.')])
    
    class RentForm(FlaskForm):
        years = FieldList(FormField(YearForm), min_entries=10)

The template:模板:

form.html
    for year in rentForm.years:
        <p>{{ year.label }}: {{ year(size=32) }}
            {% for error in year.errors %}
                <span style="color: red;">[{{ error }}]</span>
            {% endfor %}
        </p>

It looks like I'm accessing the fields incorrectly in the template.看起来我在模板中错误地访问了这些字段。 What is the correct way?正确的方法是什么? And how can I give the fields more descriptive labels and variable names?以及如何为字段提供更多描述性标签和变量名称?

Any help would be greatly appreciated;任何帮助将不胜感激; thanks!谢谢!

EDIT: if this could be more easily done in another language please let me know.编辑:如果这可以更容易地用另一种语言完成,请告诉我。 I just started learning Flask/Python and am not married to either.我刚开始学习 Flask/Python,也没有结婚。

I am doing something similar.我正在做类似的事情。 Perhaps my example will help.也许我的例子会有所帮助。 I am extending the form, in your case RentForm.years in my Flask handler in Python, NOT in my template.我在 Python 中的 Flask 处理程序中扩展了表格,在你的情况下是 RentForm.years,而不是在我的模板中。 Here is my form...这是我的表格...

class TemplateFormRow(FlaskForm):
    col = StringField(label='Column Name')
    data_type = SelectField(label='Data Type',
                            choices=[("boolean", "boolean"), ("datetime", "datetime"),
                                     ("integer", "integer"), ("decimal", "decimal"), ("string", "string")])
    sequence = HiddenField()
    delete = SubmitField(label='Delete')


class TemplateForm(FlaskForm):
    rows = FieldList(unbound_field=FormField(TemplateFormRow))
    add_row = SubmitField(label='Add Row', render_kw={'class': "btn btn-primary"})
    confirm = SubmitField(label='Save', render_kw={'class': "btn btn-primary"})
    btn_cancel = SubmitField(label='Cancel', render_kw={'formnovalidate': True, 'class': "btn btn-primary"})

Notice that in my case I put, on the parent form, a button that allows the user to add another row.请注意,在我的例子中,我在父表单上放置了一个允许用户添加另一行的按钮。 You'd handle it a bit differently if you always want 10 rows.如果您总是想要 10 行,则处理方式会有所不同。

Here is part of the Python code that works with this form.这是适用于此表单的 Python 代码的一部分。 The append_entry line is particularly important to you... append_entry 行对您来说特别重要...

if request.method == 'POST':如果 request.method == 'POST':

if form.btn_cancel.data:
    return redirect(url_for('admin'))

if form.add_row.data:  # User pressed the add row button
    form.rows.append_entry()

and here is the Python code that renders the template...这是呈现模板的 Python 代码......

return render_template(template_name_or_list='generic_entry_page.html', page_hdr='Template',
                       show_form=form, form_msg=msg)

Finally, here is the part where my template processes this...最后,这是我的模板处理这个的部分......

                {% for element in show_form %}
                    {% if element is iterable %}  {# This is a vertical set of forms #}
                        <table>
                            <tr>
                                {% for field in element[0] %}
                                    {#                                      {% for field in element.rows[0] %}#}
                                    {% if field.type != 'HiddenField' and field.label.text != 'CSRF Token' %}
                                        <th>{{ field.label }}</th>
                                    {% else %}
                                        <th>{{ field }}</th>
                                    {% endif %}
                                {% endfor %}
                            </tr>
                            {% for row in element %}
                                <tr>
                                    {% for field in row %}
                                        <td>
                                            {% if field.type == 'SubmitField' %}
                                                <button {{ field }} {{ field.label.text }} </button>
                                            {% else %}
                                                {{ field }}
                                            {% endif %}
                                        </td>
                                    {% endfor %}
                                </tr>
                            {% endfor %}
                        </table>
                        <br>

The resultant screen looks like this...生成的屏幕如下所示... 点击添加行按钮添加一行

Does this help?这有帮助吗?

Okay I was able to make a satisfactory toy example work using the information from this question .好的,我能够使用此问题中的信息制作出令人满意的玩具示例。

The python: python:

class ImageForm(FlaskForm):
    frequency = SelectField(choices=[('monthly', 'Monthly'),('weekly', 'Weekly')])
    caption = StringField('Caption')
    credit = StringField('Credit')

class TestForm(FlaskForm):
    images = FieldList(FormField(ImageForm), min_entries=10)

The template:模板:

<form action="" method="post">

    {{ testForm.hidden_tag() }}

    <table>
    {% for image in testForm.images %}
        <tr>
            <td> {{ image['frequency'] }} </td>
            <td> {{ image['caption'] }} </td>
            <td> {{ image['credit'] }} </td>
        </tr>
    {% endfor %}
    <table>
    
</form>

The result: Rendered HTML (ignore all the tabs; I have a problem)结果:渲染 HTML (忽略所有选项卡;我有问题)

I think this should suit my needs nicely.我认为这应该很好地满足我的需求。

PS: And you can access individual fields like so: PS:您可以像这样访问各个字段:

testForm.images[0]['frequency']

For the first dropdown menu.对于第一个下拉菜单。

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

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