Django paging technology django-pagination and Paginator, djangopaginator

Source: Internet
Author: User

Django paging technology django-pagination and Paginator, djangopaginator

Please note the source before reprint: http://blog.csdn.net/gugugujiawei

I. Overview

Almost all web applications require the paging function, but the paging technology is divided into two types, one is to load all, stored in the browser cache, and the other is paging access, partially load. The advantage of the previous method is that it is easy to implement and can be used in some specific applications with few project requirements. Plug-ins that can be used, such as datatables, the advantage of the latter method is that it does not occupy too much cache on the server. This technology is required for list items with a large amount of data. Because my current project belongs to the management backend and manages data from thousands of users, the second method can only be used. I didn't think so much during initial development, So I simply used the datatables plug-in to solve it. (datatables plug-in is a good plug-in. improvement should be available in the second method ).


2. Load django paging part

In the initial research, I wanted to use boow.paginator. However, I felt that it was not very useful. The advantage was that there were many resources on the Internet. If you wanted to do a great job, consider it. Then we found django-pagination.

1. django-pagination

This is a python package. It is easy to use from a github project. Reference http://www.cnblogs.com/bbcar/p/3521103.html

But this is a lazy tool, okay (tool-wise ). However, when multiple pages need to be paginated, it won't work. either modify the source code of django-pagination and change its url to point, but I didn't study it. When the project involves migration, you need to know that installing various things is a disadvantage, and you need to modify the source code, it is not worth the candle. As a result, the page plug-in provided by django-Paginator.

2. Paginator

First look at the effect


It's barely possible. Google is a better choice.

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 used to differentiate pages on the same page.

My.html

The data retrieval operation is as follows:

{% for u in midware.object_list %}{%  endfor %}

Insert the Code where you want to insert a tab:

<div style="float:right">       <span style="white-space:pre"></span>{%  include "pages/midware.html" %}</div> 
Of course, it is also possible to directly move midware.html from the original block, but to decouple it, create another folder, pages, and put the html on pages.

Midware.html

<Ul class = "pagination" >{% if midware. has_previous %} <li> <a href = "? Table = 1 & page = {midware. previus_page_number }}" class = "prev" >{{ previus_link_decorator | safe }} previous page </a> </li >{% else %} <li class = "paginate_button previous disabled"> <span class = "disabled prev" >{{ previus_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 ">{{ page }}</a> </ li >{% endifequal %}{% else %}< li>... </li> {% endif %} {% endfor %} {% if midware. has_next %} <li> <a href = "? Table = 1 & page = {midware. next_page_number }}" class = "next"> next page {next_link_decorator | safe }}</a> </li >{% else %} <li class = "paginate_button next disabled"> <span class = "disabled next"> next page: {next_link_decorator | safe }}</span> </li >{% endif %} </ul>

The role of table is embodied here. Of course, I'm suffering ~

In fact, when writing this paging column, I have been thinking about a problem: how to reuse the midware.html code, because this part of the code is only two different places, one is the object name passed from the view, the second is the table value, which is used most of the time. It is imperative to adopt this method.

Tried these methods:

1. Read an html file and replace the strings, such as midware and table. Try document.write,innerhtml,('your id'your .html () for javascript and html file operations. Merge ().

In the div of the cursor, this method is successful. But the essence (feeling) of the problem is finally discovered-it is impossible to achieve it.

This is because when the browser accesses django, it will return the html file, {}, {%}, and other things, and then call the javascript code, any operation using javascript code is a little late.

Therefore, only one html can be written in each paging column at the end. If anyone has other good methods, please kindly advise me. I feel that if django's built-in {% include 'html' %} method, how nice is it if there is a parameter passing function.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.