我无法通过查询获取实体
我使用的程序与我之前的项目中使用的程序相同,这是我从 Google 提供的教程中学到的,但这次我没有得到任何结果。
我的代码是从我的数据存储中获取 3 个随机笑话,这使得只有 100 个笑话和将它们显示在 HTML 表格中。
这是我的模型:
class joke(db.Model):
jokeID = db.IntegerProperty()
content = db.TextProperty()
这是我的控制器中的代码,我在其中获取实体:
def get(self):
deck = range(1, 101)
shuffle(deck)
items = list()
itemCounter = 0
for jokeNumber in deck:
itemCounter += 1
if itemCounter <= 3:
self.response.out.write(jokeNumber)
# I tried with fetching from the model & with GqlQuery
#items.append(joke.all().filter('jokeID=',jokeNumber).fetch(1))
items.append(db.GqlQuery("SELECT * FROM joke WHERE jokeID=:1",jokeNumber))
else:
break
template_values = {'items' : items}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path,template_values))
&这里是我用控制器中的数据填充 HTML 表的地方:
<table border="0" width="800px">
{% for item in items %}
<form method="post" action="/ocena">
<tr>
<td>
{{ item.content }}
</td>
</tr>
</form>
{% endfor %}
</table>
在站点的源代码中,我得到了三个空单元格,但是当我在 GAE 的数据存储查看器中执行查询时,我得到了所需的结果。
I was using the same procedure that I was using in my previous projects, that I learned from the tutorials provided by Google, but this time I'm getting no results..
My code is to get 3 random jokes from my datastore, which keeps only 100 jokes & to show them in a HTML table.
Here is my model:
class joke(db.Model):
jokeID = db.IntegerProperty()
content = db.TextProperty()
Here is the code in my controller in which I'm getting the entities:
def get(self):
deck = range(1, 101)
shuffle(deck)
items = list()
itemCounter = 0
for jokeNumber in deck:
itemCounter += 1
if itemCounter <= 3:
self.response.out.write(jokeNumber)
# I tried with fetching from the model & with GqlQuery
#items.append(joke.all().filter('jokeID=',jokeNumber).fetch(1))
items.append(db.GqlQuery("SELECT * FROM joke WHERE jokeID=:1",jokeNumber))
else:
break
template_values = {'items' : items}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path,template_values))
& here is where I fill the HTML table with the data from the controller:
<table border="0" width="800px">
{% for item in items %}
<form method="post" action="/ocena">
<tr>
<td>
{{ item.content }}
</td>
</tr>
</form>
{% endfor %}
</table>
In the source from the site I'm getting three empty cells, but when I execute the query in the Datastore Viewer from GAE I get the desired results..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我检查了你的示例,这段代码对我有用:
I've checked your example, and this code works for me:
如果您有类别,我认为上面的代码可以从数据存储中的类别中获取一个随机笑话。
I think the above code would work to get one random joke from a category in the datastore if you have categories.