db.Model.all() returns 一个空列表

db.Model.all() returns an empty list

这是我的 tips.html 模板的片段,包含在 index.html:

{% for t in tips %}
<div class="col-lg-4 col-sm-6">          
  <form action="comment.html" method="get" class="portfolio-box">
    <img src="img/portfolio/4.jpg" class="img-responsive" alt="">
    <div class="portfolio-box-caption">
      <div class="portfolio-box-caption-content">
        <div class="project-category text-faded">
          {{ t.title }}
        </div>
        <div class="project-name">
          {{ t.content }}                                                                       
        </div>
        <div><br /></div>
        {% if user != 'None' %}
          <input type="hidden" name="tipTitle" value="{{ t.title }}">
          <input type="hidden" name="tipContent" value="{{ t.content }}">
          <input type="hidden" name="hparam" value="tips">
          <div><button class="btn btn-default btn-l wow tada">Explore</button></div>
        {% endif %}
        </div>
      </div>
    </form>          
   </div>
{% endfor %}

html 的这个块应该显示数据存储中可用的提示。但是在我将第一个提示添加到数据存储库并将响应从 addtip.html 重定向到 index.html 之后,我只得到一个带有图片的 div 但没有 {{ t.title }} 也没有 {{ t.content }} 直到我刷新页面。

我将 Tip.all() 作为 tips 传递给 index.html

models.py

from google.appengine.ext import db
class User(db.Model):
    fullName = db.StringProperty()
    username = db.StringProperty()
    email = db.StringProperty()    
    password = db.StringProperty()
    def getKey(self):
        return self.key()    
class Tip(db.Model):
    title = db.StringProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    img = db.BlobProperty()
    user = db.ReferenceProperty(User, collection_name='tips') 
    def getKey(self):
        return self.key()    
class Comment(db.Model):
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    user = db.ReferenceProperty(User, collection_name='comments')
    tip = db.ReferenceProperty(Tip, collection_name='comments')

我的经纪人

class MainPage(BaseHandler):         
    def get(self):        
        template = JINJA_ENVIRONMENT.get_template('index.html')
        query = Tip.all()            
        context = {                   
                'title' :'OutgoingIndex - ' + currentUser.username,
                'user' : currentUser.username,
                'tips' : query
                }
        self.response.write(template.render(context))

看来您是 Eventual Consistency 的受害者:

Consistency in the context of the GAE datastore, and in very simple, non-technical, terms, is 'availability of the latest copy of the data'. Perhaps a more intutive (but blatantly incorrect) definition would be the 'lack of lag between writes and reads'. Strongly consistent means minimal to no lag, where written data is instantly available for a subsequent read. Eventually consistent means there is considerable lag, where written data will be 'eventually' available to reads after it has been propagated and stored in multiple datacenters.

所以发生的事情是,您将在写入完成之后但在它传播之前返回到提示列表,因此此时数据不在索引中。如果您在重定向之前等待几秒钟,或者像您一样刷新,数据就会在那里。

这很正常,您会习惯的。但是,如果您使用 ancestor query. If you're new to this topic, this article could be interesting: Balancing Strong and Eventual Consistency with Google Cloud Datastore.

,数据存储会保证查询高度一致