首页 > 解决方案 > FlaskWTF 多个表单相同的页面,带有 1 个提交按钮

问题描述

我正在尝试创建一些东西,用户可以在其中将选定数量的人添加到数据库中。唯一(现在)被问到的是人名。

我通过执行以下操作创建此表单:

        <form method="post" action="{{ url_for('add_person') }}">
            {{ form.csrf_token }}
            {% for i in range(0, amount) %}
                {{ form.plafond.label }}
                {{ form.plafond }}
                </br>
            {% endfor %}
            <input type="submit" value="Submit">
        </form>

和:

class new_person(FlaskForm):
    name = StringField('Name')

如何获取此人在每个字段中输入的个人数据?

标签: pythonflaskflask-wtformswtforms

解决方案


您需要在表单中添加这些字段,假设它们是字符串(更改您使用的数据类型)

class new_person(FlaskForm):
    name = StringField('Name')
    csrf_token =StringField('csrf')

然后在提交时通过(在您的 add_person 方法中)访问它:

form = new_person()
form.csrf.data # This will give you the csrf data

推荐阅读