如何分页数据集

时间:2011-07-10 17:39:57

标签: python django google-app-engine django-templates pagination

您好我想启用以前工作的Web分页,并且在更新环境或我的实现(不确定哪个)时它已经崩溃了。我想对GAE订购的列表进行分页 由于对象往往具有“自然顺序”,即时间,数字,单词等,这些代码可以在某处可用吗?我尝试过类似谷歌在此列出的Joey G的例子:http://code.google.com/appengine/articles/paging.html 我努力从URL查询中获取名为bookmark的参数:

  next = None
  bookmark = self.request.get("bookmark") 
  category = self.request.get('cg')#category parameter
  if bookmark:
    bookmark = datetime.strftime(bookmark[:-7], "%Y-%m-%d %H:%M:%S")  
  else:       
    bookmark = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") 

  if cg: 
    articles = Articles.all().filter("category =", cg).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)    

它曾经工作,现在分页被破坏,可能是因为我不完全了解如何处理modified和datetime对象。你能在途中帮助我吗?我的用例的URL是www.koolbusiness.com/li 感谢

EDIT / UPDATE:这是显示列表页面的当前代码。它只在少数几个地方使用IN所以据说可以在没有IN的情况下重写它的工作难度是由于可能的许多组合分支如此之多,即用nocategory或类别搜索,或用没有搜索也没有地理空间,但我不认为我正在尝试一些不可能的东西只是需要学习更多的python(涉及lambda编程,我只是粘贴并且没有完全掌握)并且更清晰处理所有组合时的结构。

class I18NListPage(FBBaseHandler,I18NHandler):

  def get(self, cursor=None, limit=60, PAGESIZE = 10, twittername = None):
    client = OAuthClient('twitter', self) 
    if client.get_cookie():
      info = client.get('/account/verify_credentials')
      twittername = info['screen_name']
    if (users.is_current_user_admin()):
       timeline = datetime.now () - timedelta (days = limit)   
    else:
        timeline = datetime.now () - timedelta (days = limit)
    logo = ''             
    if util.get_host().endswith('.br'):
      cookie_django_language = 'pt-br'
      logo = 'montao'   
      translation.activate(cookie_django_language)
      self.request.COOKIES['django_language'] = cookie_django_language
      dispatch= 'template/montaoli.html'
    else:
        cookie_django_language = self.request.get('cookie_django_language', '') if self.request.get('cookie_django_language', '') else self.request.get('hl', '')
        dispatch= 'template/li.html'
    if cookie_django_language:
      if cookie_django_language == 'unset':
        del self.request.COOKIES['django_language']
      else:
        self.request.COOKIES['django_language'] = cookie_django_language
      self.reset_language()       
    next = None
    bookmark = self.request.get("bookmark") 
    if not bookmark:
       bookmark = = str(time.time())

    category = self.request.get('cg')
    q = self.request.get('q').encode("utf-8")
    w = self.request.get('q')
    cg = self.request.get('cg')
    t = self.request.get('t') 
    f = self.request.get('f')

    if cg and not t and not q and not f:#category without search
        ads = Ad.all().filter("category =", cg).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)    
    elif q and not t and not cg and not f:#search without category
        ads = Ad.all().search(self.request.get('q')).filter("published =", True)    
        ads = filter(lambda x: x.modified > timeline, ads)
        ads = filter(lambda x: x.modified <= bookmark, ads)
        ads = ads[:PAGESIZE+1]
        ads = sorted(ads, key=lambda x: x.modified, reverse=True)
        #iterate list keeping elements that are on timeline newer than bookmark
    elif q and not t and cg and not f:
        ads = Ad.all().search(q).filter("type =", 's').filter("category =", cg).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)    
    elif t and not f:
        ads = Ad.all().filter("type =", 'w').filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)    
    elif f == 'c':
        ads = Ad.all().filter("company_ad =", True).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)
    elif f == 'p':
        ads = Ad.all().filter("company_ad =", False).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)
    else:
        if util.get_host().find('onta') > 1:
            ads = Ad.all().filter("modified >", timeline).filter("published =", True).filter("url IN", ['www.montao.com.br','montao']).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)              
        else:
            ads = Ad.all().filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)
    if util.get_host().find('onta') > 1 and f == 'c':
        ads = Ad.all().filter("company_ad =", True).filter("modified >", timeline).filter("published =", True).filter("url IN", ['www.montao.com.br','montao']).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)
    elif util.get_host().find('onta') > 1 and f == 'p':
        ads = Ad.all().filter("company_ad =", False).filter("modified >", timeline).filter("published =", True).filter("url IN", ['www.montao.com.br','montao']).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1)  


    if self.request.get('lat'):
        m=int(self.request.get('r')) if self.request.get('r') else 804670
        logging.info(m)
        lat = self.request.get('lat')
        lon = self.request.get('lon') if self.request.get('lon') else self.request.get('lng')
        ads = Ad.proximity_fetch(Ad.all().filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified") ,db.GeoPt(lat, lon),max_results=PAGESIZE+1, max_distance=m)
        ads = sorted(ads, key=lambda x: x.modified, reverse=True)
    if ads and len(ads) == PAGESIZE+1:
      next = ads[-1].modified
      ads = ads[:PAGESIZE]     

    template_values = {'twittername':twittername,'request':self.request,'lat':self.request.get('lat'),'lon':self.request.get('lon'),'q':q,'w':w,'cg':cg,'t':t,'logo':logo,'ads':ads, 'next':next, 'user':users.get_current_user(), 'bookmark':bookmark,'q':q, 'user_url': users.create_logout_url(self.request.uri) if users.get_current_user() else 'login',
            'cg':category,'admin':users.is_current_user_admin(),}
    template_values.update(dict(current_user=self.current_user, facebook_app_id=FACEBOOK_APP_ID))
    path = os.path.join(os.path.dirname(__file__), dispatch)
    self.response.out.write(template.render(path, template_values))

更新2:我尝试使用django paginator类from paginator import Paginator, InvalidPage, EmptyPage,在这种情况下,以下代码实际上为数据集分页:

        articles = Articles.all()
        paginator = Paginator(articles,PAGESIZE)
        articles = paginator.page(page)

所以我发现这个解决方案很有吸引力,因为它非常简单易读,希望你可以对它进行评论。

2 个答案:

答案 0 :(得分:3)

Query cursors正是为此而构建的。您应该使用它们而不是构建自己的解决方案。

答案 1 :(得分:1)

在将书签传递给客户端之前,您可能需要对其进行base64编码。 我尝试过[您的网站] [1],但如果没有服务器端错误消息,则无法清楚地回答您的问题。

此外,正如传呼文章所提到的,直接使用日期时间作为书签将导致问题,同时在同一时间段内发布了多篇文章(在您的情况下,时间段将是一分钟)。您可能需要考虑使用此格式。

2008-10-26 04:38:00|aee15ab24b7b3718596e3acce04fba85

或使用秒来表示时间。

1310427763.47|aee15ab24b7b3718596e3acce04fba85
  如果它是第一个被查看的页面,我应该将它设置为datetime.now()吗?

您只需忽略修改后的过滤器即可获得第一页结果。 例如:

if bookmark:
    suggestions = Suggestion.all().order("-when")
        .filter('when <=', bookmark).fetch(PAGESIZE+1)
else:
    suggestions = Suggestion.all().order("-when").fetch(PAGESIZE+1)

或使用当前时间作为书签。

import time
bookmark = str(time.time())