我正在尝试为谷歌应用引擎数据存储中的实体实现“编辑”功能。因此,如果为数据存储实体指定了一个键,我想用与该键关联的值填写一个表单。如果未指定任何键,则这些值将从先前的表单发布尝试中提取。虽然我可以使用 self.request.arguments() 循环遍历 self.request 字段,但我不知道如何使用数据存储区 (db.get(key)) 对象执行此操作。
所以请让我知道这是否可能,或者即使我的 gae 表单处理还很遥远。
class ItemsPage(webapp2.RequestHandler):
def __init__(self, *args, **kwargs):
super(ItemsPage, self).__init__(*args, **kwargs)
self.formerrors = dict()
self.formdata = dict()
def get(self):
if self.request.get('key'):
# Key specified, get field values from db store
key = self.request.get('key')
item = db.get(key)
for field in item.arguments(): # THIS DOES NOT WORK
self.formdata[field] = item.field
else:
# No key specified, use the self.request values
for field in self.request.arguments(): # THIS DOES WORK
self.formdata[field] = self.request.get(field)
# render the template with the formdata
template_values = {
'formerrors': self.formerrors,
'formdata': self.formdata # Use formdata to populate form fields
}
template = JINJA_ENVIRONMENT.get_template(
'templates/items.html'
)
self.response.write(template.render(template_values))
def post(self):
if self.validateSortForm():
# Form input is valid, do some processing and then put into datastore
item = itemDatstore()
item.put()
self.redirect('/')
else():
# Form input was not valid, redisplay the form
self.get()
谢谢