URLs
URL (r'^index/(\d*)', Views.index)
Views
fromDjango.shoutcutsImportRender fromapp impost Models fromAppImportCommon fromDjango.utils.safestringImportMake_safedefIndex (request,page): Page= Common.try_int (page,1) Per_item= 5#Show 5 per pageStart = (page-1) *Per_item End=page-*Per_item Count=models. Host.objects.all (). Count () result=models. Host.objects.all () [Start:end] Sum=-(-count//per_item)#total number of pageslinks=[] First_link='<a href ='/index/{}'> Home <a>'. Format (1) links.append (first_link) Prev_link='<a href ='/index/{}'> Prev <a>'. Format (page-1) links.append (prev_link) forIinchRange (sum):ifpage = = I+1: Link='<a style='clour:red;'href ='/index/{}'>{}<a>'. Format (i+1,i+1) Else: Link='<a href ='/index/{}'>{}<a>'. Format (i+1,i+1) links.append (link) next_link='<a href ='/index/{}'> Next <a>'. Format (page+1) links.append (next_link) End_link='<a href ='/index/{}'> Last <a>'. Format (sum) links.append (end_link) page= Make_safe ("'. Join (links))#to turn a string into a code that can be displayedRET ={'Count': Count,'Data': result,'page':p Age}returnRender (Request,'index.html', ret)
Common
def Try_int (arg,default): Try : = Int (ARG) expect expection: = default return arg
View Code
Simple page-Paging steps