我在Google App Engine中有一个表单,我想在该表单中同时上传图片和所有文本。我是否必须将此分为两个单独的页面和操作?
这是我的上传处理程序:
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def upload(self, reseller_id, imgfile):
upload_files = imgfile
blob_info = upload_files[0]
key = blob_info.key()
r = Reseller.get_by_id(reseller_id)
r.blob_key_logo = str(key)
r.put();
这是我创建的新经销商对象:
class NewReseller(BaseHandler):
def get(self):
if self.user:
self.render("new_reseller.html")
else:
self.redirect("/display_resellers")
def post(self):
name = self.request.get('name')
website = self.request.get('website')
information = self.request.get('information')
address = self.request.get('address')
city = self.request.get('city')
state = self.request.get('state')
zipcode = self.request.get('zipcode')
email = self.request.get('email')
phone = self.request.get('phone')
r = Reseller( name = name,
website = website,
information = information,
address = address,
city = city,
state = state,
zipcode = zipcode,
email = email,
phone = phone)
r.put()
theresellerid = r.key().id()
#And then Upload the image
u = UploadHandler()
logo_img = u.get_uploads('logo_img')
u.upload(theid, logo_img)
self.redirect('/display_resellers')
我认为我的问题是这条线:
logo_img = u.get_uploads('logo_img')
它弹出错误信息
for key, value in self.request.params.items():
AttributeError: 'NoneType' object has no attribute 'params'
我以某种方式需要这个NewReseller类从BlobstoreUploadHandler继承.getuploads,所以我可以这样做:
logo_img = self.get_uploads('logo_img')
或者可能有更好的方法,因为这似乎有些混乱。
所以我的问题是如何在一页上以一种形式上载文件和数据。我可以用两个单独的页面来做。一种用于添加代理商,另一种用于添加映像,但这似乎过于复杂。
我试图从这个问题中遵循一些步骤和线索:
Upload files in Google App Engine
****** Edit *****下面的工作实现:
class EditReseller(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
def get(self, reseller_id):
if self.user:
reseller = Reseller.get_by_id(int(reseller_id))
upload_url = blobstore.create_upload_url('/upload')
image = True
if reseller.blob_key_logo is None:
image = False
self.render('edit_reseller.html', r=reseller, reseller_id=reseller_id, upload_url=upload_url, image=image)
else:
self.redirect('/admin')
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
reseller_id = self.request.get('reseller_id')
upload_files = self.get_uploads('logo_img')
if upload_files:
blob_info = upload_files[0]
key = blob_info.key()
r = Reseller.get_by_id(int(reseller_id))
r.blob_key_logo = str(key)
r.put();
name = self.request.get('name')
website = self.request.get('website')
information = self.request.get('information')
address = self.request.get('address')
city = self.request.get('city')
state = self.request.get('state')
zipcode = self.request.get('zipcode')
email = self.request.get('email')
phone = self.request.get('phone')
if name and website and information and email and phone and address and city and state and zipcode:
r = Reseller.get_by_id(int(reseller_id))
r.name = name
r.website = website
r.information = information
r.address = address
r.city = city
r.state = state
r.zipcode = zipcode
r.email = email
r.phone = phone
r.put()
else:
error = "Looks like your missing some critical info"
self.render("edit_reseller.html", name=name, website=website, information=information, address=address, city=city, zipcode=zipcode, email=email, phone=phone, error=error)
self.redirect("/edit_reseller/" + reseller_id)
最佳答案
您只需要将UploadHandler的逻辑放入Reseller(BaseHandler)内,并使Reseller继承自blobstore_handlers.BlobstoreUploadHandler。
由于NewReseller类没有从BlobstoreUploadHandler继承,因此对get_uploads的调用失败。 BlobstoreUploadHandler类接管了上载操作,因此您无需创建post方法,只需从post中添加相应的逻辑(name = self.request.get('name')
,r = Reseller()
,r.put()
等),然后将其添加到上载方法中即可。
您不应手动调用或创建新的处理程序实例(除非您知道自己在做什么),因为它会丢失使它工作的所有内容。
官方文档中的complete app sample可能也有帮助。