当我更新 GAE 数据存储时,只有在浏览器刷新页面后才会显示正确的数据存储内容:
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
import webapp2
#def testkey():
# return db.Key.from_path('test', 'test')
class TestEntity(db.Model):
testkey = db.StringProperty(multiline=False)
testvalue = db.StringProperty(multiline=False)
class TestRefreshProblem(webapp2.RequestHandler):
def get(self):
testquery = TestEntity.all()#.ancestor(testkey())
entities = testquery.run()
template_values = {
'entities': entities,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class TestRefreshProblemPost(webapp2.RequestHandler):
def post(self):
# testEntity = TestEntity(parent=testkey())
testEntity = TestEntity()
testEntity.testkey = self.request.get('testkey')
testEntity.testvalue = self.request.get('testvalue')
testEntity.put()
self.redirect('/')
app = webapp2.WSGIApplication([
('/', TestRefreshProblem),
('/pst', TestRefreshProblemPost)
], debug=True)
index.html 是:
<html>
<body>
<table border=0>
<tr><td width=200>Key</td><td width=200>Value</td></tr>
{% for entity in entities %}
<tr>
<td width=200>{{ entity.testkey|escape }}</td>
<td width=200>{{ entity.testvalue|escape }}</td>
</tr>
{% endfor %}
</table>
<form action="/pst" method="post">
<table>
<td ><input type="text" name="testkey" size=30/></td>
<td ><input type="text" name="testvalue" size=30/></td>
<tr><td><input type="submit" value="Add entity"></td></tr>
</table>
</form>
</body>
</html>
通过使用(虚拟)祖先(re:带有#的行),问题消失了。在我看来这是一种奇怪的行为......没有祖先可以解决吗?