custom pagination
view view function:
def index_page(request,page): #Pagination view display_linkpage_count = 5 #Number of page options displayed at the bottom of the page display_item = 10 #Display data items per page count = Host.objects.all().count() #Total number in the database all_page, current_page, start_item, end_item = postion(count, display_item, page) #Call the postion function to process result = Host.objects.all()[start_item:end_item] #Get the data to be displayed on the current page current_count= Host.objects.all()[start_item:end_item].count() #Current page displays data items page_html = pager(current_page, all_page,display_linkpage_count) #Call custom paging function ret = {'data':result,'count':count,'current_count':current_count,'page_html':page_html} return render(request,'ajax_app/index.html',ret)
routing:
url(r'^index_page/(?P<page>\d*)', views.index_page, name='index_page')
Two custom functions:
from django.utils.safestring import mark_safe #Safely output the string to the front end def postion(count,display_item,page): all_page = int((count + display_item - 1) / display_item) # The total number of pages, round up an integer, such as 5/2 is 3 try: current_page = int(page) if current_page <= 0: # If negative, defaults to 1 current_page = 1 elif current_page > all_page: current_page = all_page except Exception, e: current_page = 1 # current page ID, default is 1 finally: pass # The data obtained by Host.objects.all() is returned as a list, starting from 0 # 0--9 Host.objects.all()[0:10] The first 10 data # 10--19 Host.objects.all()[10:20] # 20--29 Host.objects.all()[20:30] start_item = (current_page - 1) * display_item end_item = current_page * display_item return all_page,current_page,start_item,end_item def pager(current_page,all_page,display_linkpage_count): #Custom paging #current_page is the current page, all_page is the total number of pages, and display_linkpage_count is the page number link to be displayed below temp_html = [] first_html = "<a href='/ajax_app/index_page/%d'>首页</a>" % (1) temp_html.append(first_html) if current_page <=1: prev_html = "<a href='#'>上一页</a>" else: prev_html = "<a href='/ajax_app/index_page/%d'>上一页</a>" % (current_page - 1) temp_html.append(prev_html) begin = current_page - (display_linkpage_count / 2) end = current_page + (display_linkpage_count / 2) if all_page <= display_linkpage_count: #When the total number of pages is less than the number of pages to be displayed below the link begin = 1 end = all_page else: if begin == 0: end = current_page + (current_page - begin + 1) begin = 1 elif begin < 0: end = current_page + (current_page - begin + 1)+1 begin = 1 elif end > all_page: begin = current_page - (end - current_page + 1) end = all_page for i in range(begin,end+1): if current_page == i: a_html = "<a href='/ajax_app/index_page/%d' style='color: chartreuse'>%d</a>" % (i,i) #If it is the current page, display green else: a_html = "<a href='/ajax_app/index_page/%d'>%d</a>" % (i, i) temp_html.append(a_html) if current_page >= all_page: next_html = "<a href='#'>下一页</a>" else: next_html = "<a href='/ajax_app/index_page/%d'>下一页</a>" % (current_page+1) temp_html.append(next_html) end_html = "<a href='/ajax_app/index_page/%d'>尾页</a>" % (all_page) temp_html.append(end_html) temp_str = mark_safe('-'.join(temp_html)) # Use the mark_safe method to statically output the string, and use the join method to concatenate the list into a string return temp_str
html page:
<body> <h1>Host Information</h1> <table border="1"> <tr> <td>hostname</td> <td>IP address</td> </tr> {% for item in data %} <tr> <td>{{ item.hostname }}</td> <td>{{ item.IP }}</td> </tr> {% endfor %} </table> <div> <td>{{ page_html }}</td> </div> <div>Current page count: {{ current_count }}</div> <div>Total number: {{ count }}</div> </body>