在Google App Engine中实现无限滚动

时间:2012-09-08 21:47:37

标签: jquery python google-app-engine

首先,我想说明我之前曾问过类似的问题,但是我想更多地指出这个问题。作为python,app引擎,特别是jquery的菜鸟,这将是一项相当艰巨的任务。我想知道是否有人能指出我正确的方向(也许是在应用程序引擎本身这样做的方式),甚至可能非常友好地概述我应该在这里做什么。

以下是在我的个人资料页面中发布帖子的代码:

我现在拥有的是Query对象:

 posts = db.GqlQuery("select * from Profile_Comments WHERE name_of_profile =:1 order by date_created desc", profile_id)

我的jinja模板中的for循环获取帖子:

{% for post_tmp in posts %}

        {{post_tmp.post.replace('\r\n', '<br>') | safe }}

{%  endfor %}

非常感谢你!抱歉伪重新发布!

编辑 - 整个配置文件处理程序(模板在上面发布...它是一个简单的for循环):

  class Profile(MainHandler):
    def get(self, profile_id):
        u = User.by_name(profile_id.lower())
        if not u:
            self.redirect('/')
        if self.user and profile_id:
            current_user = self.user.name
            name1 = ''
            friend_name = ''
            team_imagee = ''
            key = ''
            monthss = ''
            yearss = ''
            dayss = ''
            countryss = ''
            team_imagee2 = ''
            imgs2 = ''
            imgs = ''
            key2 = ''
            name22 = ''
            name2 = ''
            handless = ''
            msgg = ''
            imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", profile_id)
            imgs2  = db.GqlQuery("select * from Profile_Images WHERE name =:1", current_user)
            team_name  = db.GqlQuery("select * from Teams WHERE name =:1", profile_id)
            team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", profile_id)
            team_images2  = db.GqlQuery("select * from Teamimg WHERE user =:1", current_user)
            friends  = db.GqlQuery("select * from Friends WHERE name =:1 order by added_date desc limit 10", profile_id)
            posts = db.GqlQuery("select * from Profile_Comments WHERE name_of_profile =:1 order by date_created desc", profile_id)
            name2 = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            month = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            day = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            year = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            country = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            handle  = db.GqlQuery("select * from Handle WHERE name =:1", profile_id)
            rating = db.GqlQuery("select * from Rating WHERE name_of_profile =:1", profile_id)
            profile_msg = db.GqlQuery("select * from Profile_Msg WHERE name =:1", profile_id)
            for handles in handle:
                handless = handles.handle_name
            for months in month:
                monthss = months.month
            for countrys in country:
                countryss = countrys.country
            for days in day:
                dayss = days.day
            for years in year:
                yearss = years.year
            for clan in team_name:
                    name1 = clan.team_name_anycase
            for clan in team_name:
                    name2 = clan.team_name
            for image in team_images:
                team_imagee = image.key()
            for image2 in team_images2:
                team_imagee2 = image2.key()
            for img in imgs:
                key = img.key()
            for img2 in imgs2:
                key2 = img2.key()
            for msg in profile_msg:
                    msgg = msg.msg       
            self.render('profile.html', msg = msgg, team_name2 = name2, handle_var = handless,  imgs = imgs, team_img2 = team_imagee2, profile_image_posted = key2, posts = posts, profile_id = profile_id, country_var = countryss, day_var = dayss, year_var = yearss, month_var = monthss, current_user = current_user, friends = friends, team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
        else:
            self.redirect('/register')


    def post(self, profile_id):
        if self.user and profile_id:
            current_user = self.user.name
            name1 = ''
            friend_name = ''
            team_imagee = ''
            key = ''
            monthss = ''
            yearss = ''
            dayss = ''
            countryss = ''
            team_imagee2 = ''
            imgs2 = ''
            imgs = ''
            key2 = ''
            name22 = ''
            name2 = ''
            handless = ''
            msgg = ''
            imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", profile_id)
            imgs2  = db.GqlQuery("select * from Profile_Images WHERE name =:1", current_user)
            team_name  = db.GqlQuery("select * from Teams WHERE name =:1", profile_id)
            team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", profile_id)
            team_images2  = db.GqlQuery("select * from Teamimg WHERE user =:1", current_user)
            friends  = db.GqlQuery("select * from Friends WHERE name =:1 order by added_date desc limit 10", profile_id)
            posts = db.GqlQuery("select * from Profile_Comments WHERE name_of_profile =:1 order by date_created desc", profile_id)
            name2 = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            month = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            day = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            year = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            country = db.GqlQuery("select * from User WHERE name =:1", profile_id)
            handle  = db.GqlQuery("select * from Handle WHERE name =:1", profile_id)
            for handles in handle:
                handless = handles.handle_name
            for months in month:
                monthss = months.month
            for countrys in country:
                countryss = countrys.country
            for days in day:
                dayss = days.day
            for years in year:
                yearss = years.year
            for clan in team_name:
                    name1 = clan.team_name_anycase
            for clan in team_name:
                    name2 = clan.team_name
            for image in team_images:
                team_imagee = image.key()
            for image2 in team_images2:
                team_imagee2 = image2.key()
            for img in imgs:
                key = img.key()
            for img2 in imgs2:
                key2 = img2.key()
            profile_submit_comment=''
            post_check = self.request.get('profile_submit_comment')
            if self.request.get('delete'):
                the_post = self.request.get('delete')
                q = Profile_Comments.get_by_id(int(the_post), parent=None)
                db.delete(q)
                error = "Comment successfully deleted."
                self.render('profile.html', handle_var = handless, team_name2 = name2, error = error, imgs = imgs, team_img2 = team_imagee2, profile_image_posted = key2, posts = posts, profile_id = profile_id, country_var = countryss, day_var = dayss, year_var = yearss, month_var = monthss, current_user = current_user, friends = friends, team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
#            if self.request.get('edit'):
#                the_post = self.request.get('edit')
#                q = Profile_Comments.get_by_id(int(the_post), parent=None)
#                db.delete(q)
#                self.redirect('/profile/%s' %profile_id)
            elif not post_check:
                error = "You cannot submit empty posts."
                self.render('profile.html', team_name2 = name2, error = error, imgs = imgs, team_img2 = team_imagee2, profile_image_posted = key2, posts = posts, profile_id = profile_id, country_var = countryss, day_var = dayss, year_var = yearss, month_var = monthss, current_user = current_user, friends = friends, team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
            elif self.request.get("profile_submit_comment") != '':
                profile_comments = Profile_Comments (name_of_submitted = self.user.name, name_of_profile = profile_id)
                commentupl = self.request.get("profile_submit_comment")
                profile_comments.post = (commentupl)
                profile_comments.put()
                self.redirect('/profile/%s' %profile_id)

1 个答案:

答案 0 :(得分:2)

使用“无限滚动”的主要原因之一是减少加载页面时最初需要发送的数据量。我可能错了,但看起来你有一个加载所有用户帖子的Gqlquery。对于无限滚动概念,这并没有多大意义。如果在加载页面时查询了所有帖子,那么最初不显示所有帖子的好处很少(如果有的话)。

无论如何,我会考虑使用jQuery插件,例如Infinite Scroll。这是基本概念。

首先加载页面并打印从GqlQuery返回的内容。

<html>
  <div id="posts">
    <div>post 1</div>
    <div>post 2</div>
    <div>post 3</div>
  </div>
</html>

接下来使用javascript选择<div id="posts">元素。

然后,将另一组帖子(第2页)加载到关闭DOM <div>

最后,当有人到达页面底部时,请将您选择的<div id="posts">元素附加到下一组帖子中(将它们插入<div id="posts">元素中的最终帖子下方。)< / p>