用数据填充 WTForms FormField FieldList 会在字段中生成 HTML

Posted

技术标签:

【中文标题】用数据填充 WTForms FormField FieldList 会在字段中生成 HTML【英文标题】:Filling WTForms FormField FieldList with data results in HTML in fields 【发布时间】:2015-06-13 10:04:38 【问题描述】:

我有一个 Flask 应用程序,我可以在其中通过上传 CSV 文件来填充表单数据,然后读取该文件。我想用从 CSV 读取的数据填充一个 FieldList。但是,当我尝试填充数据时,它会将原始 html 输入到 TextFields 中,而不仅仅是我想要的值。我做错了什么?

app.py

from flask import Flask, render_template, request, url_for
from flask.ext.wtf import Form
from wtforms import StringField, FieldList, FormField, SelectField
from wtforms.validators import DataRequired
from werkzeug.datastructures import MultiDict

app = Flask(__name__)
app.config['SECRET_KEY']='asdfjlkghdsf'

# normally student data is read in from a file uploaded, but for this demo we use dummy data
student_info=[("123","Bob Jones"),("234","Peter Johnson"),("345","Carly Everett"),
              ("456","Josephine Edgewood"),("567","Pat White"),("678","Jesse Black")]

class FileUploadForm(Form):
    pass

class StudentForm(Form):
    student_id = StringField('Student ID', validators = [DataRequired()])
    student_name = StringField('Student Name', validators = [DataRequired()])

class AddClassForm(Form):
    name = StringField('classname', validators=[DataRequired()])
    day = SelectField('classday', 
                      choices=[(1,"Monday"),(2,"Tuesday"),(3,"Wednesday"),(4,"Thursday"),(5,"Friday")],
                      coerce=int)

    students = FieldList(FormField(StudentForm), min_entries = 5) # show at least 5 blank fields by default

@app.route('/', methods=['GET', 'POST'])
def addclass():
    fileform = FileUploadForm()
    classform = AddClassForm()

    # Check which 'submit' button was called to validate the correct form
    if 'addclass' in request.form and classform.validate_on_submit():
        # Add class to DB - not relevant for this example.
        return redirect(url_for('addclass'))

    if 'upload' in request.form and fileform.validate_on_submit():
        # get the data file from the post - not relevant for this example.
        # overwrite the classform by populating it with values read from file
        classform = PopulateFormFromFile()
        return render_template('addclass.html', classform=classform)

    return render_template('addclass.html', fileform=fileform, classform=classform)

def PopulateFormFromFile():
    classform = AddClassForm()

    # normally we would read the file passed in as an argument and pull data out, 
    # but let's just use the dummy data from the top of this file, and some hardcoded values
    classform.name.data = "Super Awesome Class"
    classform.day.data = 4 # Thursday

    # pop off any blank fields already in student info
    while len(classform.students) > 0:
        classform.students.pop_entry()

    for student_id, name in student_info:
        # either of these ways have the same end result.
        #
        # studentform = StudentForm()
        # studentform.student_id.data = student_id
        # studentform.student_name.data = name
        #
        # OR
        student_data = MultiDict([('student_id',student_id), ('student_name',name)])
        studentform = StudentForm(student_data)

        classform.students.append_entry(studentform)

    return classform


if __name__ == '__main__':
    app.run(debug=True, port=5001)

templates/addclass.html

<html>
    <head>
        <title>Flask FieldList Demo</title>
    </head>
    <body>
        <h1>Add Class</h1>
        % if fileform %
        <div>
            <p>Add class from file:</p>
            <form action="" method="post" enctype="multipart/form-data" name="fileform">
                 fileform.hidden_tag() 
                <p><input type="submit" name="upload" value="Upload"></p>
            </form>
        </div>
        <hr>
        % endif %
        <div>
            <form action="" method="post" name="classform">
                 classform.hidden_tag() 
                Class Name:  classform.name <br>
                Day:  classform.day <br>
                <br>
                <div>
                    <table>
                        <tr>
                            <th> Student Number </th>
                            <th> Name </th>
                        </tr>
                        % for student in classform.students %
                        <tr>
                            <td> student.student_id </td>
                            <td> student.student_name </td>
                        </tr>
                        % endfor %
                    </table>
                </div>
                <p><input type="submit" name="addclass" value="Add Class"></p>
            </form>
        </div>
    </body>
</html>

有问题的代码 sn-p 发生在 classform.students.append_entry(studentform) 行。 如果需要,我可以发布输出 HTML。我期望的是这样的: 我得到的是:

【问题讨论】:

【参考方案1】:

好的,我花了好几个小时在这上面,最后它只是一个微不足道的代码更改。

大多数字段都允许您通过修改data 属性来更改它们的值(就像我在上面所做的那样)。事实上,在我的代码中,我有这样的评论:

    ### either of these ways have the same end result.
    #
    # studentform = StudentForm()
    # studentform.student_id.data = student_id
    # studentform.student_name.data = name
    #
    ### OR
    #
    # student_data = MultiDict([('student_id',student_id), ('student_name',name)])
    # studentform = StudentForm(student_data)

但是,对于 FormFields 的 FieldList,我们不应编辑 data 属性,而应编辑字段本身。以下代码按预期工作:

for student_id, name in student_info:

    studentform = StudentForm()
    studentform.student_id = student_id     # not student_id.data
    studentform.student_name = name

    classform.students.append_entry(studentform)

我希望这可以帮助遇到同样问题的人。

【讨论】:

我不认为你对完整的独立工作示例有一个要点? 此工作示例的要点是here。 太棒了,我以错误的方式实现了这个,并且很难从数据库中填充以进行编辑。感谢发帖!【参考方案2】:

响应接受的答案: append_entry 函数需要数据,而不是表单。 因此,如果您像这样处理它,您的代码也可以按您的预期工作。额外的好处是更易于维护

# First remap your list of tuples to a list of dicts
students = [dict(zip(["student_id","student_name"], student)) for student in student_info]
for student in students:
    # Tell the form to add a new entry with the data we supply
    classform.students.append_entry(student)

【讨论】:

有趣的是,WTForms 文档说 append_entry([data]) 说 [data] 是可选的,但没有明确说明它应该是字典 - wtforms.readthedocs.io/en/2.2.1/fields/…

以上是关于用数据填充 WTForms FormField FieldList 会在字段中生成 HTML的主要内容,如果未能解决你的问题,请参考以下文章

如何在带有 FormField 的 Flask / WTForms 中使用 populate_obj?

使用来自 SQLAlchemy 对象的数据在烧瓶中预填充 WTforms

WTForms-如何预填充文本区域字段?

使用从前一个字段中选择的值填充 WTForms 选择字段

Flask WTForms 动态表单依赖项 selectField 未填充在编辑页面中

itextpdf FormField 生成pdf修改字体大小