How to populate my WTForm variables?

房东的猫 提交于 2020-01-12 12:24:52

问题


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... )

回答1:


You need to pass your object via the form's second argument, "obj":

editAdForm = AdForm(obj=ad)

Outlined in the documentation crash course here: http://wtforms.simplecodes.com/docs/dev/crash_course.html#editing-existing-objects



来源:https://stackoverflow.com/questions/8128238/how-to-populate-my-wtform-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!