【发布时间】:2014-06-11 08:00:56
【问题描述】:
我想在其模板上直接显示实体被查看的次数。例如,给定这个Book ndb.Model:
class Book(ndb.Model):
title = ndb.StringProperty()
view_count = ndb.IntegerProperty(default = 0)
由这个处理:
class BookPage(MainHandler):
def get(self, book_id):
book = Book.get_by_id(int(book_id))
self.render(book-page.html, book = book)
这里是 book-page.html:
<h2>{{book.title}} | {{book.view_count}}</h2>
一种方法是我们可以设置view_count = view_count + 1,然后设置book.put(),但这会涉及大量的数据库写入。
一定有更好的办法。
【问题讨论】:
标签: google-app-engine python-2.7 jinja2 app-engine-ndb webapp2