我无法通过查询获取实体

发布于 2024-12-04 13:05:11 字数 1339 浏览 1 评论 0原文

我使用的程序与我之前的项目中使用的程序相同,这是我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2024-12-11 13:05:11

我检查了你的示例,这段代码对我有用:

def get(self):
    deck = range(1, 101)
    shuffle(deck)
    items = db.GqlQuery("SELECT * FROM joke WHERE jokeID IN :1", deck[:3])
    template_values = {'items' : items}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path,template_values))

I've checked your example, and this code works for me:

def get(self):
    deck = range(1, 101)
    shuffle(deck)
    items = db.GqlQuery("SELECT * FROM joke WHERE jokeID IN :1", deck[:3])
    template_values = {'items' : items}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path,template_values))
可是我不能没有你 2024-12-11 13:05:11

我的代码是随机获取 3 个

def get_random_joke(self, category):
    jokeinfo = JokeData.all().filter("category =", category)
    return jokeinfo[random.randint(0, jokeinfo.count()-1)] 

如果您有类别,我认为上面的代码可以从数据存储中的类别中获取一个随机笑话。

My code is to get 3 random

def get_random_joke(self, category):
    jokeinfo = JokeData.all().filter("category =", category)
    return jokeinfo[random.randint(0, jokeinfo.count()-1)] 

I think the above code would work to get one random joke from a category in the datastore if you have categories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文