任何人都对如何在Google App Engine django模板上显示带有分页的Google Datastore记录表有所了解或示例?像Gmail分页一样。
答案 0 :(得分:2)
class gaePaginator ():
leftPages = 3
rightPages = 3
centerPages = 1
totalPages = 1
baseURL = 'http://example.com/%s/'
moveLeft = '<'
moveLeftmax = '<<'
moveRight = '>'
moveRightmax = '>>'
href = '<a href="%s">%s</a>'
currentPageWrap = '<b>%s</b>'
textBetween = '...'
separator = ' '
currentPage = 1
"""
Ranges for current page
"""
def leftRange(self):
return range(1, self.leftPages+1)
def rightRange(self):
return range (self.totalPages - self.rightPages+1, self.totalPages + 1)
def centerRange(self):
return range(self.currentPage - self.centerPages, self.currentPage + self.centerPages + 1)
def renderLeftNav(self):
"""
Method render left page nav tab like <<(move to first page) <(move to previous page).
"""
result = []
if self.currentPage <= 1 or self.currentPage > self.totalPages:
# draw nothing
pass
elif self.currentPage == 2:
#draw only arrow for previous page
arrow = self.href % (self.baseURL % 1, self.moveLeft)
result.append(arrow)
else:
#draw arrow for previous page and first page
arrow = self.href % (self.baseURL % 1, self.moveLeftmax)
result.append(arrow)
arrow = self.href % (self.baseURL % (self.currentPage - 1), self.moveLeft)
result.append(arrow)
return self.separator.join(result)
def renderRightNav(self):
"""
Method render left page nav tab like "<<(move to first page) <(move to previous page)"
"""
result = []
if self.currentPage >= self.totalPages or self.currentPage < 1:
#draw nothing
pass
elif self.currentPage == self.totalPages - 1:
#draw only arrow for next page
arrow = self.href % (self.baseURL % self.totalPages, self.moveRight)
result.append(arrow)
else:
#draw arrow for next page and for last page
arrow = self.href % (self.baseURL % (self.currentPage + 1), self.moveRight)
result.append(arrow)
arrow = self.href % (self.baseURL % self.totalPages, self.moveRightmax)
result.append(arrow)
return self.separator.join(result)
def getRange (self):
result = []
for i in range(1, self.totalPages+1):
if i in self.leftRange() or i in self.rightRange() or i in self.centerRange():
result.append(i)
else:
if result[-1] is not None:
result.append(None)
return result
def renderPageRange (self):
'''
Method returns list of pages as urls in HTML representation.
'''
result = []
for i in self.getRange():
if i == self.currentPage:
tmp = self.currentPageWrap % i
else:
if i is None:
tmp = self.textBetween
else:
tmp = self.href % (self.baseURL % i, i)
result.append(tmp)
return self.separator.join(result)
def asURL(self):
'''
Method returns list of pages as urls in HTML representation with border arrows.
'''
result = []
result.append(self.renderLeftNav())
result.append(self.renderPageRange())
result.append(self.renderRightNav())
return self.separator.join(result)
在views.py中使用:
from ....main.paginator import gaePaginator
pages = gaePaginator ()
pages.currentPage = pagesNumber
pages.leftPages = 2
pages.rightPages = 2
pages.centerPages = 1
pages.totalPages = totalPagesNumber
pages.baseURL = 'page/%s'
pages.separator = ' '
result = {'pages' : pages.asURL(),}
在模板中使用:
{{ pages }}
结果你会收到: 十页,
当前页面为1: 1 2 ... 9 10&gt; &GT;&GT;
当前页面是4:&lt;&lt; &LT; 1 2 3 4 5 ... 9 10> &GT;&GT;
当前页面是6:&lt;&lt; &LT; 1 2 ... 5 6 7 ... 9 10> &GT;&GT;
当前页面是10:&lt;&lt; &LT; 1 2 ... 9 10
<< - moves to first page
< - moves to previous page
> - moves to next page
>> - move to last page
此外,此代码尝试为任何情况绘制页面:例如,如果用户将设置页码= -1或100000“1 2 ... 9 10”将被渲染。
它也可以像django Paginator一样用在模板中,但我没有尝试过。
答案 1 :(得分:0)
答案 2 :(得分:0)
对于在Google App Engine中寻找简单分页的其他人,请尝试:
http://appengine-cookbook.appspot.com/recipe/simple-pagination-djangocorepaginator/