问题
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