我有一个网页,正在将数据库中的记录提供给 Google App Engine 中的 Django html 模板。是否可以异步加载帖子,例如当用户在页面上向下滚动 10 个帖子时,它会加载另外 10 个帖子?我应该使用某种 jQuery 在模板中执行此操作,还是异步数据库获取?
class MainHandler(webapp2.RequestHandler):
def get(self):
records_query = db.GqlQuery("SELECT * FROM Record order by date desc")
records = records_query.fetch(10)
self.response.out.write(records_query)
template_values = {
'records': records,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class Page(webapp2.RequestHandler):
def get(self,page):
numberOfPages = int(page)
records_query = db.GqlQuery("SELECT * FROM Record order by date desc")
records = records_query.fetch(numberOfPages * 10)
records = records[((numberOfPages- 1) * 10):]
template_values = {
'records': records,
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
在模板中,它只是对从 DB 发送的记录进行的普通循环
{% for record in records %}
{{ record.title }} {{ record.body }}
{% endfor %}