Reprinted please indicate the source!
Author: Yu XiaoYu
Link: http://blog.csdn.net/zgyulongfei/article/details/8842338
You need to create a paging function in the blog. Many materials on the Internet say that the paginator class is used for implementation. However, I have read some examples and think that this paging method is very inefficient when the data volume is very large.
For example, the following method is used in a blog (http://xiaobin268.iteye.com/blog/391237:
Def list (request): after_range_num = 5 bevor_range_num = 4 try: page = int (request. get. get ("page", 1) print ('page -----> ', page) If page <1: page = 1 partition t valueerror: page = 1 info = article. objects. order_by ('id '). all () paginator = paginator (Info, 3) Try: articlelist = paginator. page (PAGE) Partition T (emptypage, invalidpage, pagenotaninteger): articlelist = paginator. page (1) print ('articlelist ----> ', articlelist. object_list) # display range: If page> = after_range_num: page_range = paginator. page_range [page-after_range_num: Page + bevor_range_num] else: page_range = paginator. page_range [0: int (PAGE) + bevor_range_num] Return render_to_response ("blogsite/list.html", locals ())
In this way, you only need to execute article. Objects. All () every time you click Next or previous page, and this method Retrieves all records from the database.
Think about it. If there are 100000 records in the database and only 10 records are displayed on each page, When I click Next, in order to view 10 records, we need to query 100000 data records before paging. If many users access the website at the same time, they click Next page, and the next page is quickly clicked... So, can't the database survive?
Therefore, for the sake of database health, this article will introduce a method that is much more efficient than above to reduce the burden on the database.
First, if you only need to display 20 data records on one page, you only need to query 20 records from the database, as shown in the following code:
Obtain the data on the first page:
posts = BlogPost.objects.all()[0:20]
Obtain the data on the second page:
posts = BlogPost.objects.all()[20:40]
The data acquisition method after the third page is similar.
This method is different from the following:
posts = BlogPost.objects.all()posts = posts[0:20]
The former uses the inertia of querysets, which means that queries are executed only after the database is evaluated, which is faster than immediate query. This kind of inertia utilizes the shard function of Python. blogpost. Objects. All () [0: 20] uses 0 as offset and 20 as limit in the actual query, which can greatly improve the query performance. Reference blog (http://www.redicecn.com/html/blog/Django/2011/0502/268.html)
While the latter blogpost. Objects. All () is to first query all the data from the database, and then split the posts [0: 20], which is a waste of electricity and environmental protection.
The efficient paging code is shown below:
One_page_of_data = 20def getblogposts (RQ): Try: curpage = int (RQ. get. get ('curpage', '1') allpage = int (RQ. get. get ('allpage', '1') pagetype = STR (RQ. get. get ('pagetype ', '') failed t valueerror: curpage = 1 allpage = 1 pagetype = ''# click Next page or previous page. If pagetype = 'pageid ': curpage + = 1 Elif pagetype = 'pageup': curpage-= 1 startpos = (curpage-1) * one_page_of_dataendpos = startpos + one_page_of_dataposts = blogpost. objects. all () [startpos: endpos] If curpage = 1 and allpage = 1: # mark 1 allpostcounts = blogpost. objects. count () allpage = allpostcounts/counters = allpostcounts % reply remainpost> 0: allpage + = 1 return render_to_response ("blog.html", {'posts': posts, 'allpage': allpage, 'curpage': curpage}, context_instance = requestcontext (RQ ))
Here we will briefly describe # mark 1: In order to reduce database operations, we can only query the total number of database records when the web page is loaded for the first time, and calculate the total number of pages. After that, the total number of page numbers will be obtained from get.
The following HTML is pasted:
<HTML lang = "ZH-CN">
----------
EOF