I have a web page that’s being served records from the DB to a Django html template in Google App Engine. Is it possible to do this loading of posts asynchronously, like when a user scrolls down 10 posts on the page, it loads another 10? Should i do this in the template with some kind of jQuery or is it an asynchronous DB fetch?
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))
In the template its just the ordinary looping through the records sent from DB
{% for record in records %}
{{ record.title }} {{ record.body }}
{% endfor %}
This is my solution. It now loads asyncrously when you scroll to the bottom. Its based on the second example from this site
Template.html:
And this is the method in main,py which loads the posts from DB.
This is the template thats loaded into the main template.
posts.html