class IndexHandler(BaseRequestHandler):
def get(self, page=1):
print('iiiiiiiiiiiiiiiii')
current_time = time.time()
conn = ORM.session()
all_count = conn.query(ORM.News).count()
obj = Pagination(page, all_count)
current_user_id = self.session['user_info']['nid'] if self.session['is_login'] else 0
result = conn.query(ORM.News.nid,
ORM.News.title,
ORM.News.url,
ORM.News.content,
ORM.News.ctime,
ORM.UserInfo.username,
ORM.NewsType.caption,
ORM.News.favor_count,
ORM.News.comment_count,
ORM.Favor.nid.label('has_favor')).join(ORM.NewsType, isouter=True).join(ORM.UserInfo, isouter=True).join(ORM.Favor, and_(ORM.Favor.user_info_id == current_user_id, ORM.News.nid == ORM.Favor.news_id), isouter=True)[obj.start:10]
conn.close()
str_page = obj.string_pager('/index/')
self.render('home/index.html', str_page=str_page, news_list=result,current_time=current_time)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Pagination:
def __init__(self, current_page, all_item):
try:
page = int(current_page)
except:
page = 1
if page < 1:
page = 1
all_pager, c = divmod(all_item, 5)
if c > 0:
all_pager += 1
self.current_page = page
self.all_pager = all_pager
@property
def start(self):
return (self.current_page - 1) * 5
@property
def end(self):
return self.current_page * 5
def string_pager(self, base_url="/index/"):
list_page = []
if self.all_pager < 11:
s = 1
t = self.all_pager + 1
else: # 总页数大于11
if self.current_page < 6:
s = 1
t = 12
else:
if (self.current_page + 5) < self.all_pager:
s = self.current_page - 5
t = self.current_page + 5 + 1
else:
s = self.all_pager - 11
t = self.all_pager + 1
# 首页
# first = '<a href="%s1">首页</a>' % base_url
# list_page.append(first)
# 上一页
# 当前页 page
if self.current_page == 1:
prev = '<a href="javascript:void(0);">上一页</a>'
else:
prev = '<a href="%s%s">上一页</a>' % (base_url, self.current_page - 1,)
list_page.append(prev)
for p in range(s, t): # 1-11
if p == self.current_page:
temp = '<a class="active" href="%s%s">%s</a>' % (base_url,p, p)
else:
temp = '<a href="%s%s">%s</a>' % (base_url,p, p)
list_page.append(temp)
if self.current_page == self.all_pager:
nex = '<a href="javascript:void(0);">下一页</a>'
else:
nex = '<a href="%s%s">下一页</a>' % (base_url, self.current_page + 1,)
list_page.append(nex)
# 尾页
# last = '<a href="%s%s">尾页</a>' % (base_url, self.all_pager,)
# list_page.append(last)
# 跳转
# jump = """<input type='text' /><a onclick="Jump('%s',this);">GO</a>""" % ('/index/', )
# script = """<script>
# function Jump(baseUrl,ths){
# var val = ths.previousElementSibling.value;
# if(val.trim().length>0){
# location.href = baseUrl + val;
# }
# }
# </script>"""
# list_page.append(jump)
# list_page.append(script)
str_page = "".join(list_page)
return str_page