我想使用flask,WTForm和flask_sqlalchemy建立一个网站。有一个供用户通过WTForm提交信息的网页,现在我想创建另一个页面来显示数据库中的所有数据。我想使用WTForm做到这一点。在一本书中,示例如下:>
def GetSchoolFormById(school_id):
school = orm.School.query.get(int(school_id))
if school is None:
return None
schoolform = SchoolForm()
schoolform.id.data = school.id
schoolform.name.data = school.name
schoolform.area_id.data = school.area_id
schoolform.area_name = school.area.name
schoolform.teachdesc.data = school.teachdesc
schoolform.address.data = school.address
schoolform.schooltype_id.data = school.schooltype_id
schoolform.schooltype_name = school.schooltype.name
schoolform.website.data = school.website
schoolform.distinguish.data = school.distinguish
schoolform.leisure.data = school.leisure
schoolform.threashold.data = school.threashold
schoolform.partner.data = school.partner
schoolform.artsource.data = school.artsource
schoolform.feedesc.data = school.feedesc
schoolform.longitude.data = school.longitude
schoolform.latitude.data = school.latitude
schoolform.schoolimages = school.schoolimages
schoolform.feature_ids.data = [x.feature_id for x in school.schoolfeatures]
schoolform.area_id.choices = g_choices_area
schoolform.schooltype_id.choices = g_choices_schooltype
schoolform.feature_ids.choices = g_choices_feature
return schoolform
但是对于我来说,我使用了FieldList,所以我不知道如何像示例一样添加数据。
class ProjectForm(FlaskForm):
project_id = HiddenField('项目ID')
project_name = StringField('工程名称')
project_ST = DateField('起始时间', format='%Y-%m-%d')
project_FT = DateField('截止时间', format='%Y-%m-%d')
class ItemsForm(Form):
item_id = HiddenField('事件ID')
item_name = StringField('事件名称')
pre_item = SelectMultipleField('前驱事件')
# 暂定为string类型,加到date的%d里,具体我也不会,实现不了可以再改
last_time = StringField('持续时间')
class MainForm(FlaskForm):
items = FieldList(
FormField(ItemsForm)
)
@app.route('/view_items', methods=['GET', 'POST'])
def view_items():
form_one = ProjectForm()
form_two = MainForm()
if request.method == 'GET':
project = DBProject.query.filter_by(project_id=1).first().__dict__
print(project)
# 由于目前只有一个project,所以直接导入全部items数据
items = list(DBItem.query.filter().all())
form_one.project_id.data = project['project_id']
form_one.project_name.data = project['project_name']
form_one.project_ST.data = project['project_ST']
form_one.project_FT.data = project['project_FT']
for item in items:
print(item)
form_two.items.append_entry(ItemsForm(item))
return render_template('view_items.html', form_one=form_one, form_two=form_two)
当我测试代码时,像这样从底部开始的第二行有一个错误:
TypeError: formdata should be a multidict-type wrapper that supports the 'getlist' method
我该如何处理?我应该使用其他方法而不是WTForm将数据传递到html页面吗?
答案 0 :(得分:0)
WTForms表单的__init__方法的参数如下__init__(formdata=None, obj=None, prefix='', data=None, meta=None, **kwargs)
。您正在传递一个Sqlalchemy对象作为formdata参数,该参数旨在接收通过http帖子作为Werkzeug Multidict发送的用户表单数据。如果将对象作为obj参数传递,则只要数据库列名称与表单字段名称匹配,它就应该起作用。您也可以使用这种技术来初始化ProjectForm,而不是依次设置每个字段的数据。
project = DBProject.query.filter_by(project_id=1).first()
form_one = ProjectForm(obj=project)
items = list(DBItem.query.filter().all())
for item in items:
form_two.items.append_entry(ItemsForm(obj=item))