【问题标题】:Error when trying to assign blob object尝试分配 blob 对象时出错
【发布时间】:2011-03-25 17:41:06
【问题描述】:

以下代码

for h in hits:
    urls.append(h['url'])
    result = db.Blob(urlfetch.Fetch(h['url']).content)
    model.image = result

返回错误

不能连接 'str' 和 'NoneType' 对象。

【问题讨论】:

    标签: python django google-app-engine


    【解决方案1】:

    使用一些调试打印来确定urlfetch.Fetch(h['url']).content 是否甚至返回任何内容。根据错误,结果为Nonedb.Blob() 期望结果为字符串。

    如果是这样,请在尝试应用之前通过检查content 的值进行相应的计划。或许还有一点错误跟踪可以更好地衡量?

    这是一个简单的例子:

    errors = []
    for h in hits:
        urls.append(h['url'])
        content = urlfetch.Fetch(h['url']).content
        if content is not None:
           result = db.Blob(urlfetch.Fetch(h['url']).content)
        else:
           print 'No content for', h['url']
           errors.append(h)
           continue
    
        model.image = result
    

    【讨论】:

      【解决方案2】:

      我只在这一行看到串联:

      urls.append(h['url'])

      错误判断:'String' 是 url,'NoneType' 是 h['url'] 很可能 h['url'] 为空。通过将其打印到控制台来确保这一点。

      【讨论】:

      • Python 中的字符串没有 append 方法 - urls 显然是一个列表。
      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 2018-04-03
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多