【发布时间】:2013-12-18 23:11:55
【问题描述】:
我有这个模型:
class Product(ndb.Model):
title = ndb.StringProperty()
description = ndb.TextProperty()
img = ndb.BlobKeyProperty(indexed=False)
我需要一个html表单,它读取字段的值(标题和描述)并读取图像(从文件字段),并将值保存NDB对象,图像在Blobstore中并更新字段BlobKeyProperty 正确。
当我使用 wtforms 时,我尝试使用如下形式:
class ProductForm(Form):
title = fields.TextField('Title', [validators.Required(), validators.Length(min=4, max=25)])
description = fields.TextAreaField('Description')
img = fields.FileField('img')
表单正确显示文件字段,但在 POST 中,它不起作用,因为我不知道如何读取文件、将文件保存到 Blobstore 并更新 BlobKeyProperty。
我的处理程序是这样的:
class ProductHandler(BaseHandler):
def new(self):
if self.request.POST:
data = ProductForm(self.request.POST)
if data.validate():
model = Product()
data.populate_obj(model)
model.put()
self.add_message("Product add!", 'success')
return self.redirect_to("product-list")
else:
self.add_message("Product not add!", 'error')
params = {
'form': ProductForm(),
"kind": "product",
}
return self.render_template('admin/new.html', **params)
Error is Expected str, got u'image.jpg'
如果有人可以帮助我,我将不胜感激!
【问题讨论】:
标签: blobstore app-engine-ndb wtforms