Please also indicate the source before reprint: Http://blog.csdn.net/gugugujiawei
I. Overview
Almost all Web applications, need paging functionality, but paging technology in general two, one is all loaded, stored in the browser cache, one is paged access, partially loaded. The advantage of the former method is simple, in some of the project requires a few specific applications to use more, can take advantage of plug-ins such as DataTables, the latter advantage is not to occupy the server too much cache, for the data volume of large list items must use this technology. Since my current project belongs to the management background and manages data from thousands of users, I can only use the second method. Initial development time did not think so much, directly with the DataTables plug-in is done (DataTables plugin is a good plugin oh, the improvement should be used in the second method).
Second, Django part of the page load
In the beginning of the investigation, want to use Boostrap paginator, but feel not very good, the advantages of online resources, want to do a very flashy can consider. Later found Django-pagination.
1, Django-pagination
This is a Python package that comes from a project on GitHub and is easy to use. Reference http://www.cnblogs.com/bbcar/p/3521103.html
But this is a lazy tool, OK (instrumental rationality). However, when a page has more than one need to use paging, it will not work, or modify the source code of Django-pagination, change its URL point, but I did not study, when the project involves the migration, to know to install a variety of things is a drawback, but also to modify the source code, it is not worth the candle. Thus moved to Django's own paging plugin--paginator.
2, Paginator
First look at the effect
I feel like I can barely make it. To have a more bullish X function on Google's own.
views.py
#coding: Utf-8from django.shortcuts import render_to_response,requestcontextfrom django.http import Httpresponsefrom Django.core.paginator Import Paginator,emptypage,pagenotanintegerdef Middleware_review (req): midware=[1,2,3] Midware_review=[1,2,3]limit=10page_midware=paginator (Midware,limit) page_midware_review=paginator (midware_ Review,limit) Page=req. Get.get (' page ') table=req. Get.get (' table ', ' none ') try:if table== ' 1 ': Midware=page_midware.page (page) else:midware=page_midware.page (1) except Pagenotaninteger:midware=page_midware.page (1) except Emptypage:midware=page_midware.page (page_midware.num_pages) Try:if table== ' 2 ': Midware_review=page_midware_review.page (page) else:midware_review=page_midware_review.page (1) Except Pagenotaninteger:midware_review=page_midware_review.page (1) except Emptypage:midware_review=page_midware_ Review.page (page_midware_review.num_pages) return render_to_response (' middleware-review.html ', {' Midware ': midware , ' Midware_review ': midware_review},context_instance=reqUestcontext (req))
Table is intended to distinguish between pages that are different from the same page.
my.html
Take the data operation as :
{% for u in midware.object_list%} {% endfor%}
Insert the code where you want to insert the Paging tab:
<div style= "Float:right" > <span style= "White-space:pre" ></span>{%
Of course, it is also possible to move midware.html intact, but in order to understand the decoupling, create another new folder pages and page the HTML.
Midware.html
<ul class= "pagination" >{% if midware.has_previous%} <li><a href= "? table=1&&page= {{Midware.previous_page_number}} "class=" prev ">{{Previous_link_decorator|safe}}" Previous page </a></li>{% else%} <li class= "Paginate_button previous disabled" ><span class= "Disabled prev" >{{previous_ Link_decorator|safe}} Previous page </span></li>{% endif%}{% for page in Midware.paginator.page_range%}{% if page%}{% Ifequal page midware.number%}<li class= "active" ><span class= "Current page" >{{page}}</span></li >{% else%}<li><a href= "? table=1&&page{{page_suffix}}={{page}}{{Getvars}}" class= "page" >{{p Age}}</a></li>{% endifequal%}{% else%} <li>...</li>{% endif%}{% endfor%}& nbsp; {% if Midware.has_next%} <li><a href= "? table=1&&page={{ Midware.next_page_number}} "class=" Next "> Next" {next_Link_decorator|safe}}</a></li>{% Else%} <li class= "Paginate_button Next Disabled "><span class=" disabled Next "> next {{next_link_decorator|safe}}</span></li>{% endif%} </ul>
The function of table is here. Of course, I was inspired by http://blog.csdn.net/zgyulongfei/article/details/8842338, the first time loading to find the entire database, is also very good ~
In fact, when writing this page bar, has been thinking about a question: How to reuse midware.html code, because this part of the code only two different places, one from the view of the object name, the second is the table value, most of the time in this, take now this method is also forced.
Tried these methods:
1. Read in an HTML file, replacing the string, such as midware,table. Tried document.write,innerhtml,$ (' #id '). HTML () These are used for JavaScript and HTML file operations. The general idea is to read into a file string, or a DOM object, and then use Replace to write to a div or call $ (' #id '). html ().
2, the use of JS files, in the JS file format is the case of the string, when the call in my.html, the input JS parameters, and then write back to the my.html Div, this method succeeds. But finally discovering the nature of the problem (feeling)-is impossible to achieve.
Because, when the browser accesses Django, returns the HTML file, returns the {{}} and {%}, and then calls the JavaScript code, any operation that takes the JavaScript code is slightly late.
So finally can only each page bar to write an HTML, if there are other good ways also please advise ha. Feel if Django comes with the {% include ' HTML '%} method if this is a good way to have a pass-through function.
Django Paging technology Django-pagination and Paginator