为什么我的 GQL 查询在我的 GAE 应用程序中没有返回任何结果?
我主要遵循了有关如何使用 GAE 和 Python 构建留言簿的教程。现在我只想显示特定日期的条目,但 GQL 查询不会返回任何内容(尽管有当天的条目):
class Shout(db.Model):
message= db.StringProperty(required=True)
when = db.DateTimeProperty(auto_now_add=True)
who = db.StringProperty()
class MainHandler(webapp.RequestHandler):
def get(self):
shouts = db.GqlQuery("SELECT * FROM Shout WHERE when=DATE('2010-11-05')")
# Return something without the WHERE clause
values = {'shouts':shouts}
self.response.out.write(template.render('main.html',values))
def post(self):
self.response.out.write("posted")
shout = Shout(message=self.request.get("message"),who=self.request.get("who"))
shout.put()
这是我的 main.html:
<form method="post">
<input type="text" name="who"></input>
<input type="text" name="message"></input>
<input type="submit" value="Send"> </input>
</form>
{% for shout in shouts %}
<div>{{shout.message}} from {{shout.who}} on {{shout.when}}</div>
{% endfor %}
I have mostly followed a tutorial about how to build a gustbook using GAE and Python. Now I only want to show the entries from a particular day, but the GQL query doesn't return anything (although there are entries from that day):
class Shout(db.Model):
message= db.StringProperty(required=True)
when = db.DateTimeProperty(auto_now_add=True)
who = db.StringProperty()
class MainHandler(webapp.RequestHandler):
def get(self):
shouts = db.GqlQuery("SELECT * FROM Shout WHERE when=DATE('2010-11-05')")
# Return something without the WHERE clause
values = {'shouts':shouts}
self.response.out.write(template.render('main.html',values))
def post(self):
self.response.out.write("posted")
shout = Shout(message=self.request.get("message"),who=self.request.get("who"))
shout.put()
This is my main.html:
<form method="post">
<input type="text" name="who"></input>
<input type="text" name="message"></input>
<input type="submit" value="Send"> </input>
</form>
{% for shout in shouts %}
<div>{{shout.message}} from {{shout.who}} on {{shout.when}}</div>
{% endfor %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能还有另一种方法可以解决这个问题,但我认为因为您的
when
属性是一个日期时间,所以您最好使用这样的方法:There might be another way around this, but I think that because your
when
property is a datetime you would be better served with something like this:尝试一下:
虽然可以将 Query 对象用作可迭代对象,但最好显式获取行,而不是依赖模板中的
for
来完成工作。我怀疑它不以这种方式支持。编辑:
现在我更仔细地观察这一点,我怀疑问题在于您正在查询的字段是 DateTimeProperty,并且通过使用 DATE 运算符,您实质上是在说您想要 2010-11-05 00:00:00 ,并且没有包含该确切日期和时间的记录,因此请尝试以下操作:
Try this:
While it is possible to use a Query object as an iterable, it is a better idea to explicitly fetch the rows and not depend on the
for
in your template to do the work. I suspect that it is not supported in that way.EDIT:
Now that I look more closely at this, I suspect that the problem is that the field you are querying on is a DateTimeProperty, and by using the DATE operator, you are essentially saying that you want 2010-11-05 00:00:00, and there are no records with that exact date and time, so try this instead: