标签:定义 append 2-2 += location one star src OLE
LIST=[] for i in range(1001): LIST.append(i) def user_list(request): current_page = request.GET.get(‘p‘,1) # 获取当前页数 current_page = int(current_page) per_page_count = 20 # 定义获取显示多少条数据 start = (current_page-1)*per_page_count # 获取显示开始数据 end = (current_page)*per_page_count # 获取显示结束数据 data = LIST[start:end] # 切片 all_count = len(LIST) # 获取列表的长度既总元素个数 total_page,div = divmod(all_count,per_page_count) # 根据元素获取页数 每十个元素为一页 if div: total_page += 1 # 有余数的情况下加一 page_list = [] # 定义一个页数列表 # 当前页 current_page # 总页数 total_page # 每页显示10条数据 per_page_count # 页码让显示11条 # if 总页数 <11 # start_index = 0 end_index = 总页数 # else: 如果 当前页 <= 6 start_index=1 end_index=11 # 否则 start_index=当前页-5 end_index=当前页+5+1 # 如果当前页 +5 >总页数 总页数当做最后一个值 end_index=total_page+1 page_num = 11 # 显示多少页码 if total_page < page_num: start_index = 1 end_index = total_page+1 else: if current_page <= (page_num+1)/2: start_index = 1 end_index = page_num +1 else: start_index = current_page - (page_num-1)/2 end_index = current_page + (page_num+1)/2 if (current_page+(page_num-1)/2) > total_page: end_index = total_page+1 start_index = total_page - page_num + 1 if current_page == 1: prev = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>上一页</a>" # 当上一页是第一页时什么都不干 else: prev = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>上一页</a>"%(current_page-1) #上一页 page_list.append(prev) for i in range(int(start_index),int(end_index)): # 循环页数列表 if i == current_page: temp = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>%s</a>" %(i,i) # 如果是当前页数加上样式 else: temp = "<a class=‘c1‘ href=‘/user_list.html/?p=%s‘>%s</a>" %(i,i) page_list.append(temp) if current_page == total_page: next = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>下一页</a>" else: next = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>下一页</a>" % (current_page + 1) # 下一页 page_list.append(next) jump = """ <input type=‘text‘ /><a onclick=‘jumpTo(this,"/user_list.html/?p=");‘ id=‘i1‘>Go</a> <script> function jumpTo(ths,base){ var val = ths.previousSibling.value; location.href = base+val; } </script> """ page_list.append(jump) from django.utils.safestring import mark_safe page = "".join(page_list) page = mark_safe(page) return render(request,‘user_list.html‘,{‘li‘:data,‘page‘:page,})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .c1 { display: inline-block; background-color: mediumvioletred; padding: 5px; margin: 5px; } .active{ background-color: #0e90d2; } </style> </head> <body> {% include "tag.html" %} {{ page }} </body> </html>
<ul> {% for item in li %} <li>{{ item }}</li> {% endfor %} </ul>
第二种类
新建文件夹,page_div.py 把公共函数放在里面
class Page(): def __init__(self,current_page,data_count,per_page_count=20, page_num=11): self.current_page=current_page self.per_page_count=per_page_count self.page_num=page_num self.data_count=data_count def start(self): return (self.current_page-1) * self.per_page_count def end(self): return (self.current_page)*self.per_page_count def total_page(self): total_page, div = divmod(self.data_count, self.per_page_count) # 根据元素获取页数 每十个元素为一页 if div: total_page += 1 # 有余数的情况下加一 return total_page def page_div(self): page_list = [] # 定义一个页数列表 # 当前页 current_page # 总页数 total_page # 每页显示10条数据 per_page_count # 页码让显示11条 # if 总页数 <11 # start_index = 0 end_index = 总页数 # else: 如果 当前页 <= 6 start_index=1 end_index=11 # 否则 start_index=当前页-5 end_index=当前页+5+1 # 如果当前页 +5 >总页数 总页数当做最后一个值 end_index=total_page+1 if self.total_page() < self.page_num: start_index = 1 end_index = self.total_page() + 1 else: if self.current_page <= (self.page_num + 1) / 2: start_index = 1 end_index = self.page_num + 1 else: start_index = self.current_page - (self.page_num - 1) / 2 end_index = self.current_page + (self.page_num + 1) / 2 if (self.current_page + (self.page_num - 1) / 2) > self.total_page(): end_index = self.total_page() + 1 start_index = self.total_page() - self.page_num + 1 if self.current_page == 1: prev = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>上一页</a>" # 当上一页是第一页时什么都不干 else: prev = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>上一页</a>" % (self.current_page - 1) # 上一页 page_list.append(prev) for i in range(int(start_index), int(end_index)): # 循环页数列表 if i == self.current_page: temp = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>[%s]</a>" % (i, i) # 如果是当前页数加上样式 else: temp = "<a class=‘c1‘ href=‘/user_list.html/?p=%s‘>[%s]</a>" % (i, i) page_list.append(temp) if self.current_page == self.total_page(): next = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>下一页</a>" else: next = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>下一页</a>" % (self.current_page + 1) # 下一页 page_list.append(next) jump = """ <input type=‘text‘ /><a onclick=‘jumpTo(this,"/user_list.html/?p=");‘ id=‘i1‘>Go</a> <script> function jumpTo(ths,base){ var val = ths.previousSibling.value; location.href = base+val; } </script> """ page_list.append(jump) from django.utils.safestring import mark_safe page = "".join(page_list) page = mark_safe(page) return page
from utils import page_div LIST=[] for i in range(1001): LIST.append(i) def user_list(request): current_page = request.GET.get(‘p‘,1) # 获取当前页数 current_page = int(current_page) page_obj = page_div.Page(current_page,len(LIST)) data = LIST[page_obj.start():page_obj.end()] # 切片 page=page_obj.page_div() return render(request,‘user_list.html‘,{‘li‘:data,‘page‘:page,})
标签:定义 append 2-2 += location one star src OLE
原文地址:https://www.cnblogs.com/sunhao96/p/8979542.html