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:
&l开发者_如何学Ct;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..
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))
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.
精彩评论