如何填充我的 WTForm 变量?
我正在启用可以编辑实体的功能。我想用数据存储中的变量填充表单。我该怎么做呢?我的代码未填充表单:
if self.request.get('id'):
id = int(self.request.get('id'))
ad = Ad.get(db.Key.from_path('Ad', id))
im = ad.matched_images
editAdForm = AdForm(ad)
if str(users.get_current_user()) == str(ad.user) or users.is_current_user_admin():
self.render_jinja('edit', form_url=blobstore.create_upload_url('/addimage'),
admin=users.is_current_user_admin(),
user_url= (users.create_logout_url('/'
) if users.get_current_user() else users.create_login_url(self.request.uri)),
user= users.get_current_user(),
ad= ad,
form = editAdForm)
相反,我看到此错误消息:
formdata 应该是一个多字典类型的包装器,支持 “getlist”方法:
更新
解决方法是像这样填充表单,但我想知道这是否真的是推荐的方式?
editAForm = AForm(name=article.name, title=article.title, text=article.text... )
I'm enabling a function that can edit an entity. I want to populate the form with the variables from the datastore. How can I do it? My code doesn't populate the form:
if self.request.get('id'):
id = int(self.request.get('id'))
ad = Ad.get(db.Key.from_path('Ad', id))
im = ad.matched_images
editAdForm = AdForm(ad)
if str(users.get_current_user()) == str(ad.user) or users.is_current_user_admin():
self.render_jinja('edit', form_url=blobstore.create_upload_url('/addimage'),
admin=users.is_current_user_admin(),
user_url= (users.create_logout_url('/'
) if users.get_current_user() else users.create_login_url(self.request.uri)),
user= users.get_current_user(),
ad= ad,
form = editAdForm)
Instead I see this error message:
formdata should be a multidict-type wrapper that supports the
'getlist' method:
Update
The workaround is to populate the form like this but I wonder if this really is the recommended way?
editAForm = AForm(name=article.name, title=article.title, text=article.text... )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要通过表单的第二个参数“obj”传递对象:
在此处的文档速成课程中概述:http://wtforms.simplecodes.com/docs/dev/crash_course.html#editing-existing-objects
You need to pass your object via the form's second argument, "obj":
Outlined in the documentation crash course here: http://wtforms.simplecodes.com/docs/dev/crash_course.html#editing-existing-objects