[英]How to asyncronously load posts from DB to django template on google app engine?
I have a web page that's being served records from the DB to a Django html template in Google App Engine. 我有一个网页,正在从数据库向Google App Engine中的Django html模板提供记录。 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?
是否可以异步加载帖子,例如当用户在页面上向下滚动10个帖子时又加载了10个帖子? Should i do this in the template with some kind of jQuery or is it an asynchronous DB fetch?
我应该使用某种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))
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: Template.html:
<div id="postswrapper">
<div class="item">content</div>
<div id="loadmoreajaxloader" style="display:none;"><center><img src="ajax-loader.gif" /></center></div>
</div>
<script type="text/javascript">
var pageCounter = 2;
$(window).scroll(function()
{
if (pageCounter > 0)
{
if($(window).scrollTop() == $(document).height() - $(window).height())
{
$('div#loadmoreajaxloader').show();
$.ajax({ url: "./page/" + pageCounter , success: function(html) {
if(html)
{
$("#postswrapper").append(html);
$('div#loadmoreajaxloader').hide();
pageCounter++;
}else
{
pageCounter = -1;
$('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
}
}
});
pageCounter = -1;
}
}
});
</script>
And this is the method in main,py which loads the posts from DB. 这是main,py中从数据库加载帖子的方法。
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):]
if len(records) > 0:
template_values = {
'records': records,
}
path = os.path.join(os.path.dirname(__file__), 'posts.html')
self.response.out.write(template.render(path, template_values))
This is the template thats loaded into the main template. 这是已加载到主模板中的模板。 posts.html
posts.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% for record in records %}
<div class="oneRecord">
<img src="{{ record.imageCacheURL }}" />
<a href ="./{{ record.imageID }}"> {{ record.title|escape }}</a>
</div>
{% endfor %}
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.