0

我有一个网页,正在将数据库中的记录提供给 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 %}
4

1 回答 1

1

这是我的解决方案。现在,当您滚动到底部时,它会异步加载。它基于此站点的第二个示例

模板.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>

这是 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))

这是加载到主模板中的模板。帖子.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>
于 2012-11-21T18:50:20.920 回答