我正在启用可以编辑实体的功能。我想用数据存储中的变量填充表单。我该怎么做?我的代码没有填充表单:
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 should be a multidict-type wrapper that supports the 'getlist' method:
更新
解决方法是像这样填充表单,但我想知道这是否真的是推荐的方法?
editAForm = AForm(name=article.name, title=article.title, text=article.text... )
最佳答案
您需要通过表单的第二个参数“obj”传递您的对象:
editAdForm = AdForm(obj=ad)
在此处的文档速成类(class)中概述:http://wtforms.simplecodes.com/docs/dev/crash_course.html#editing-existing-objects
关于python - 如何填充我的 WTForm 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8128238/