无法迭代数据存储区对象

时间:2014-10-12 15:18:21

标签: python google-app-engine google-cloud-datastore

我正在使用google-app-engine和python,我无法遍历数据存储区对象。

我的数据库模型:

class Page(db.Model):
    url = db.StringProperty(required=True)
    author = db.StringProperty(required=True)
    content = db.TextProperty(required=True)
    created = db.DateTimeProperty(auto_now_add = True)

这是错误发生的地方:

def generate_links(self, pages):
    urls = []
    for i, page in pages:
        urls.append(page.url + "?v=" + i)

    return urls

我正在使用以下网页获取页面:

pages = Page.all().filter("url = ", link_id).order("-created")

错误:

Traceback (most recent call last):
File "/opt/google-appengine-python/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
  rv = self.handle_exception(request, response, e)
File "/opt/google-appengine-python/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
  rv = self.router.dispatch(request, response)
File "/opt/google-appengine-python/lib/webapp2-2.5.2/webapp2.py", line 1278, in   default_dispatcher
  return route.handler_adapter(request, response)
File "/opt/google-appengine-python/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
  return handler.dispatch()
File "/opt/google-appengine-python/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
  return self.handle_exception(e, self.app.debug)
File "/opt/google-appengine-python/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
  return method(*args, **kwargs)
File "/home/Drive/main.py", line 197, in generate_links
  for i, page in pages:
TypeError: 'Page' object is not iterable

但是,我可以使用:

遍历jinja2中的页面
{% for page in pages %}
{% endfor %}

感谢您帮助我!

1 个答案:

答案 0 :(得分:0)

错误不是来自pages的迭代,而是来自将每个Page对象拆分为两个变量ipage。为了完成这项工作,您需要将其包含在enumerate

 for i, page in enumerate(pages):