【问题标题】:App Engine Python Blobstore. Error uploading imagesApp Engine Python Blobstore。上传图片时出错
【发布时间】:2019-04-02 07:59:50
【问题描述】:

尝试使用 blobstore 将图像上传到谷歌应用引擎项目时出现以下错误:

ERROR    2018-10-29 11:18:21,519 user.py:476] ERROR is 'PicUploadHandler' 
object has no attribute '_BlobstoreUploadHandler__uploads'
ERROR    2018-10-29 11:18:21,519 user.py:477] EXC is 'PicUploadHandler' 
object has no attribute '_BlobstoreUploadHandler__uploads'
Traceback (most recent call last):
  File "C:\Users\...\PycharmProjects\...\user.py", line 460, in post
    upload_files = self.get_uploads()[0]  #: 'file' is file upload field in 
the form
  File "C:\Program Files(x86)\Google\google_appengine\google\appengine\ext\webapp\blobstore_handlers.py", line 380, in get_uploads
if self.__uploads is None:
AttributeError: 'PicUploadHandler' object has no attribute '_BlobstoreUploadHandler__uploads'

我的代码如下:

class PicUploadHandler(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
"""handler to upload a profile photo to the blobstore"""

def get(self):

    try:
        user = self.user_obj()
        if user:
            upload_url = blobstore.create_upload_url('/u/profile-pic-upload')  #: create the upload url
            template_values = {
                'user': user,
                'upload_url': upload_url,
            }
            #: RENDER TEMPLATE
            self.render_template(ROUTE, '/profile-pic-upload.html', template_values)
        else:  
            self.redirect('/')

    except Exception, e:
        logging.error("ERROR is %s" % e)
        logging.exception("EXC is %s" % e)
        self.redirect("/oops")  # unexpected error

def post(self):

    try:
        user = self.user_obj()
        if user:
            self.response.headers['Content-Type'] = 'image/svg+xml'
            if user.pic_key:
                blobstore.delete(user.pic_key)  #: retrieve the old home photo and delete it
            upload_files = self.get_uploads()[0]  #: 'file' is file upload field in the form

            #: update the datastore values for the user's home photo
            user.pic_url = get_serving_url(upload_files.key())
            #: the blobstore key for later retrieval
            user.pic_key = upload_files.key()
            user.put()
            #: redirect to the profile page
            self.redirect('/u/edit-profile/')

    except Exception, e:
        logging.error("ERROR is %s" % e)
        logging.exception("EXC is %s" % e)
        self.redirect("/oops")  # unexpected error

我从一个旧项目中复制粘贴了这段代码,它运行时没有出现错误。

任何帮助将不胜感激,谢谢。

编辑: 作为 Hail Mary,我上传了代码以便检查来自实时实例的日志。似乎问题出在开发服务器/数据存储上,因为代码在应用程序在线时运行。有人知道为什么会这样吗?

【问题讨论】:

  • 您应该在您的post 部分登录upload_files。该错误可能是由于没有您期望的文件。
  • @GAEfan 谢谢你。我只是一个偶尔的程序员,为工作做一些奇怪的项目。最后一个是2年前!因此,我无法修复此错误。如果您能提供更具体的解决方法,我将不胜感激。干杯。

标签: python google-app-engine blobstore


【解决方案1】:

尝试登录看看你有什么:

import logging

user = self.user_obj()
if user:
    self.response.headers['Content-Type'] = 'image/svg+xml'
    if user.pic_key:
        blobstore.delete(user.pic_key)  #: retrieve the old home photo and delete it

    logging.error(self.get_uploads())
    logging.error(self.get_uploads()[0])
    upload_files = self.get_uploads()[0]  #: 'file' is file upload field in the form
    ...

【讨论】:

  • 谢谢,我试过了。我得到与上面相同的错误。 if self.__uploads is None: AttributeError: 'PicUploadHandler' object has no attribute '_BlobstoreUploadHandler__uploads' 相同的代码在另一个项目中运行。我已经检查并重新检查了导入和代码,并且很难过。似乎 self.get_uploads() 没有检索图像,因为错误显示它是无?我不知道,这很令人沮丧:)
  • 值得一提的是,在 dev 数据存储上,正在创建和存储 blobinfo。但它不会存储到用户模型中。
  • 作为 Hail Mary,我上传了代码以便检查来自实时实例的日志。似乎问题出在开发服务器/数据存储上,因为代码在应用程序在线时运行。有人知道为什么会这样吗?
猜你喜欢
  • 2015-02-07
  • 2013-12-25
  • 2013-06-22
  • 2013-09-14
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多