【发布时间】:2011-10-02 07:56:54
【问题描述】:
您好,我想启用曾经可以正常工作的网页分页,但它在更新环境或我的实施期间中断(不确定是哪个)。我想对 GAE 排序的列表进行分页 由于对象往往具有“自然顺序”,即时间、数字、单词等,此代码是否已经在可用的地方可用?我尝试做类似 Google 在此处列出的 Joey G 的示例:http://code.google.com/appengine/articles/paging.html 我从 URL 查询中获取名为书签的参数的努力:
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)
它曾经可以工作,现在分页已损坏,可能是因为我不完全了解如何处理修改后的日期时间对象。你能在路上帮我吗?我的用例的 URL 是 www.koolbusiness.com/li 谢谢
编辑/更新:这是显示列表页面的当前代码。它只在少数几个地方使用IN,所以据说它可以在没有IN 的情况下被重写以工作,困难在于它分支太多,因为可能有许多组合,即搜索有或没有类别,有或没有搜索的类别和也是地理空间上的,但我不认为我在尝试一些不可能的事情,只是需要学习更多 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 分页器类 from paginator import Paginator, InvalidPage, EmptyPage,以下代码实际上分页数据集:
articles = Articles.all()
paginator = Paginator(articles,PAGESIZE)
articles = paginator.page(page)
所以我觉得这个解决方案很有吸引力,因为它非常简单易读,希望您能继续发表评论。
【问题讨论】:
标签: python django google-app-engine django-templates pagination