【问题标题】:Can't iterate over GqlQuery object无法遍历 GqlQuery 对象
【发布时间】:2014-02-06 10:46:16
【问题描述】:

我有一个博客模型,其中包含 4 个实体:主题、博客、time_created 和 day_created。我查询所有值并将其分配给传递给 jinja2 模板的对象。在模板中,我遍历对象并打印每个不同的实体,从而显示每个博客文章。

数据的模型是:

class Blog(db.Model):
    subject = db.StringProperty(required = True, multiline = True)
    blog = db.TextProperty(required = True)
    time_created = db.DateTimeProperty(auto_now_add = True)
    day_created = db.DateProperty(auto_now_add = True)

我查询所有条目以显示为单独的帖子:

posts = db.GqlQuery('SELECT * FROM Blog ORDER BY time_created DESC')

然后我将它们传递给模板

class Mainpage(BaseHandler):
def get(self):
    posts = Blog_posts()
    logging.info(posts)
    if posts:
        end_time = time.time() - start_time
        logging.info(end_time)
        logging.info(start_time)

    userLogin = self.checkLogin()
    cookieVal = self.read_secure_cookie('user-id')
    if cookieVal:
        u_id = cookieVal.split('|')[0]
        usr_instance = Users.get_by_id(int(u_id))
        if usr_instance:
            name = usr_instance.username
            if userLogin == True and name:
                self.render("blog.html", posts = posts, userLogin = userLogin, user_name = name, end_time = int(end_time))
                logging.info("Logged in = " + str(userLogin))               
        else:
            self.render("blog.html", posts = posts, userLogin = userLogin, user_name = "", end_time = int(end_time))        
    else:
        self.render("blog.html", posts = posts, userLogin = userLogin, user_name = "", end_time = int(end_time)) 

函数 render() 是:

def render(self, template, **kw):
    self.response.out.write(render_str(template, **kw))

我有一个主模板 blogbase.html 我继承到所有其他页面,它包含我用于模板继承的这些行:

<div class="content">
  {% block content %}
  {% endblock %}
</div> 

在显示主博客页面并继承它的 html 文件中是:

{% extends "blogbase.html" %}

{% block content %}

    {% for post in posts %}        
      <div class="one_post">          
        <div class="subject_title">
          <div class="day">{{post.day_created.strftime("%d %b %Y")}}</div>
          <a href="/blog/{{post.key().id()}}" target="_blank">   
            {{post.subject}}
          </a>
        </div>
        <div class="post">
            {{post.blog}}
        </div>
      </div>            
    {% endfor %}
    <div class="query">
      Queried {{end_time}} seconds ago
  </div>
{% endblock %}

我添加了一个调试行以查看查询是否有效,它返回 true 并显示以下内容:

<google.appengine.ext.db.GqlQuery object at 0x7f99f03cd050>

但我无法像在模板中那样迭代这个对象,常规的 html 可以工作,但 for 循环之间的部分不会呈现。

我的查询有问题吗?还是我使用的模板引擎错了?

【问题讨论】:

    标签: google-app-engine google-cloud-datastore jinja2 gql


    【解决方案1】:

    我注意到的两件事。

    您正在模板中使用查询来迭代它并执行什么操作?打印信息?您需要获取实体。要么在查询时获取它们,要么迭代和获取。

    posts = db.GqlQuery('SELECT * FROM Blog ORDER BY time_created DESC').fetch(1000)
    

    此外,如果您将 dicts 传递给 jinja,您可能想检查您是否使用了 {% for post in posts.iteritems() %},但这里不是这种情况。

    【讨论】:

    • 根据docs GqlQUery 返回对象中的实体。是的,我将此对象传递给模板以打印出单个实体作为博客的主题、内容和日期。
    • @Bhargav 但返回查询对象而不是实体!
    • @Bhargav 进行抓取。返回的查询对象用于其他用途。其中之一是添加动态过滤器、分页等。
    • 如果我进行 fetch,它会返回什么?一个包含实体的对象,以便我可以迭代它们?
    • 谢谢你,我现在明白了,它可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多