How to populate my WTForm variables?

2019-04-04 05:30发布

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条回答
老娘就宠你
2楼-- · 2019-04-04 06:04

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

查看更多
登录 后发表回答