Django--分页
功能
web分页
使用
在项目中创建一个分页的class,web用到分页的地方直接用下面的function调用即可。
速查
class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | class Pager( object ): def __init__( self ,current_page): self .current_page = int (current_page) @property def start( self ): return ( self .current_page - 1 ) * 10 @property def end( self ): return self .current_page * 10 def page_str( self ,all_item,base_url): all_page,div = divmod (all_item, 10 ) #计算分页数量,每页显示10条数据,div是余数 if div > 0 : all_page + = 1 pager_list = [] #先定义成列表,最后再join成字符串传给前端 if all_page < = 11 : #计算start和end start = 1 end = all_page else : if self .current_page < = 6 : start = 1 end = 12 else : start = self .current_page - 5 end = self .current_page + 6 if self .current_page + 6 >all_page: start = all_page - 11 end = all_page + 1 for i in range (start,end): #生成分页的html标签,当前页变红变大显示 if i = = self .current_page: temp = '<a style="color:red;font-size:26px;" href="%s%d">%d</a>' % (base_url,i,i) else : temp = '<a href="%s%d">%d</a>' % (base_url,i,i) pager_list.append(temp) #上一页 if self .current_page> 1 : #href到当前页的上一个数字 pre_page = '<a href="%s%d">上一页</a>' % (base_url, self .current_page - 1 ) else : pre_page = '<a href="javascript:void(0)">上一页</a>' #href什么也不做 #下一页 if self .current_page > = all_page: next_page = '<a href="javascript:void(0)">下一页</a>' else : next_page = '<a href="%s%d">下一页</a>' % (base_url, self .current_page + 1 ) pager_list.insert( 0 ,pre_page) pager_list.append(next_page) #首页 if self .current_page = = 1 : home_page = '<a href="javascript:void(0)">首页</a>' else : home_page = '<a href="%s%d">首页</a>' % (base_url, 1 ) #尾页 if self .current_page = = all_page: last_page = '<a href="javascript:void(0)">尾页</a>' else : last_page = '<a href="%s%d">尾页</a>' % (base_url,all_page) pager_list.insert( 0 ,home_page) pager_list.append(last_page) #最终html字符串 return mark_safe("".join(pager_list)) |
function
1 2 3 4 5 6 7 8 | def user_list(request): current_page = request.GET.get( 'page' , 1 ) page_obj = Pager(current_page) result = models.UserList.objects. all ()[page_obj.start:page_obj.end] all_item = models.UserList.objects. all ().count() pager_str = page_obj.page_str(all_item, "/user_list/?page=" ) return render(request, 'user_list.html' ,{ 'result' :result, 'pager_str' :pager_str}) |
知识点
1、只取数据库表的前10条
1 | models.UserList.objects. all ()[ 0 : 10 ] |
2、分页是起始数和结束数
1 2 | start = (current_page - 1 ) * 10 end = current_page * 10 |
3、类的静态方法,不用加括号调用
1 2 3 4 5 6 | class pager(): @property def start( self ): return ( self .current_page - 1 ) * 10 page_obj.start #==>直接得到结果 |
4、计算分页数
1 2 3 4 | all_item = models.UserList.objects. all ().count() all_page,div = divmod (all_item, 10 ) if div > 0 : all_page + = 1 |
5、a标签点击什么也不做
1 | <a href = "javascript:void(0)" >无效标签< / a> |
6、字符串和列表拼接成html标签
字符串:
1 | html += str |
列表:
1 2 3 | list .insert( 0 ,A) - - 开头插入 list .append(A) - - 末尾插入 html = "".join( list ) |
7、xss字符串安全
1 2 | from django.utils.safestring import mark_safe return mark_safe(html_str) |
详细步骤
参照:d22page项目及video