python 生成htmltag_python – 填充WTForms FormField FieldList,数据在字段中生成HTML

这篇博客讨论了一个Flask应用,该应用使用WTForms处理表单,特别是FieldList和FormField。作者遇到的问题是在从CSV文件读取数据并尝试填充FieldList时,HTML标签被错误地插入到了TextField中,而非预期的值。文章通过代码示例展示了如何创建和验证表单,以及如何填充FieldList。问题出现在`append_entry`方法的使用上,导致了HTML的直接插入。
摘要由CSDN通过智能技术生成

我有一个Flask应用程序,我可以通过上传一个CSV文件来填充表单数据,然后读取它.我想使用从CSV读取的数据填充FieldList.但是,当我尝试填充数据时,它会将原始

HTML输入到TextField中,而不是仅仅需要我的值.我究竟做错了什么?

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)

模板/ addclass.html

Flask FieldList Demo

Add Class

{% if fileform %}

Add class from file:

{{ fileform.hidden_tag() }}


{% endif %}

{{ classform.hidden_tag() }}

Class Name: {{ classform.name }}

Day: {{ classform.day }}

Student Number Name

{% for student in classform.students %}

{{ student.student_id }}{{ student.student_name }}

{% endfor %}

违规代码段发生在classform.students.append_entry(studentform)的行.

如果需要,我可以发布输出HTML.我期望的是这样的:

我得到的是:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值