1

我在谷歌应用引擎中有一个表单,我想同时上传一张图片和我的所有文本。我是否必须将其分成两个单独的页面和操作?

这是我的上传处理程序:

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')

或者可能有更好的方法,因为这看起来有点乱。

所以我的问题是如何在一个页面上以一种形式上传文件和数据。我可以用两个单独的页面来做到这一点。一种用于添加经销商,另一种用于添加图像,但这似乎过于复杂。

我试图从这个问题中遵循一些步骤和线索:

在 Google App Engine 中上传文件

******编辑*****下面的工作实现:

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)
4

1 回答 1

2

你只需要把UploadHandler的逻辑放到Reseller(BaseHandler)里面,让Reseller继承blobstore_handlers.BlobstoreUploadHandler。

对 get_uploads 的调用失败,因为 NewReseller 类没有从 BlobstoreUploadHandler 继承。BlobstoreUploadHandler 类接管了上传操作,因此您无需创建 post 方法,只需在 post ( name = self.request.get('name'), r = Reseller(),r.put()等) 中添加相应的逻辑并将其添加到上传方法中即可。

你不应该手动调用或创建一个新的处理程序实例(除非你知道你在做什么),因为它会丢失使它工作的东西。

官方文档中的完整应用示例也可能会有所帮助。

于 2012-06-15T00:12:08.303 回答