我见过关于同一主题的类似问题,该主题是使用 Python 编写一个非常简单的博客并托管在 GAE 上。如果我错过了其中一个答案中的解决方案,我深表歉意。
我根本看不到任何正在显示的数据库条目。这是我的代码:
实体:
class Comment(db.Model):
name = db.StringProperty(required=True)
comment = db.TextProperty(required=True)
created = time.strftime("%d/%m/%Y")
主处理程序:
class MainPage(Handler):
def render_front(self, name="", comment="", error=""):
comments = db.GqlQuery("SELECT * FROM Comment ORDER BY created DESC")
self.render("front.html", name=name, comment=comment, error=error, comments=comments)
def get(self):
self.render_front()
def post(self):
name = self.request.get("name")
comment = self.request.get("comment")
if name and comment:
c = Comment(name=name, comment=comment)
c.put()
time.sleep(0.5)
self.redirect("/")
因此这将显示在 HTML 中:
{% for e in comments %}
<div class="comment">
<div class="comment-name">
{{e.name}}
</div>
<pre class="comment-content">
{{e.comment}}
<br>
on {{e.created}}
</pre>
</div>
{% endfor %}
问题是程序似乎完全忽略了上面的for block 。我设法让它工作了一段时间,但我检查了很多次,看不出问题出在哪里。
任何帮助将不胜感激。提前致谢。
最佳答案
检查此问题的几种方法:
使用管理控制台查看您的数据存储区。里面有记录吗?
您的日期时间属性存储正确吗?有关 DS 和 Datetime 的文档,请参阅此处:https://developers.google.com/appengine/docs/python/datastore/typesandpropertyclasses#DateTimeProperty
您是否已将日期设置为“dateTimeProperty”类型?请参阅此处的示例:developers.google.com/appengine/docs/python/ndb/queries 另请参阅此处:stackoverflow.com/questions/9700579/gql-select-by-date
尝试删除 GQL 中的 ORDER BY 子句。我想知道日期/时间变量是否有什么奇怪的
尝试使用 App Engine SDK 在本地运行,并使用其中的日志来查看行为。当您对一切正常进行感到满意时,您可以将其上传到 GAE。
不要使用模板引擎——现在只需在Python中完全执行循环——然后self.response.write出东西。这将告诉您查询是否正常工作。
希望它对你有用! :)
关于python - Google App Engine 上的简单博客 - 没有显示任何条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20953624/