Table of Contents

How to setup pagination using MongoDB and Django

In your views.py file...

import urllib.parse
username = urllib.parse.quote_plus('username')
password = urllib.parse.quote_plus('password')
Mongo db setup.
from pymongo import MongoClient
client = MongoClient('mongodb://%s:%[email protected]:27017'%(username,password))
db = client.ubuddy

Let us write a function which will take letter and page number as inputs...

def show_docs(request,letter,page_no)

Let us initialize a list which will contain the records to show on the site

records = []

For a give letter such as a, we want to show all the records starting with a on page 1, below code builds a Python regex to do that

session_pat = re.compile("^%s"%letter, re.IGNORECASE)

Next we want to decide on each page how many links we want to show. I am showing 20 links here.  

skipno = (int(page_no)-1)*20

Now we want to query MongoDb and get first 20 documents that I would show on page 1. Before I limit the number of docs to 20, I need to sort them. Here is how to do that...  

docs = db.session_info.find({'slug':session_pat}).sort("slug",-1).skip(skipno).limit(20)

Now you loop through the docs and do any post processing, you need to do that. In my case I have data spread between different collections so i need to query different collections. 

 for doc in docs:
    record = {}
    session = doc['session']
    key = {'session':session}
    session_main_info = collection_sessions.find_one(key)
    session_ss_info = collection_ss.find_one(key)
    if session_ss_info and session_main_info:
      record['slug'] = doc['slug']
      record['use_slug'] = session_main_info['use_slug']
      record['user'] = session_main_info['username']
    record['session'] = session
    records.append(record)

Now I have the records list ready. Lets calculate how big your pagination will be for a give letter. You query mongoDB for the total number of docs and divide it by 20.

 no_of_docs = db.session_info.find({'slug':session_pat}).count()
 no_of_pages = int(no_of_docs/20) + 1

 Now you just to build the context and return context

context = {'letter':letter,'no_of_pages':range(1,no_of_pages+1),'records':records,'page_no':page_no}
return render(request, 'paginate.html',context)

Here is the complete code...

def show_docs(request,letter,page_no):
  records = []
  session_pat = re.compile("^%s"%letter, re.IGNORECASE)
  skipno = (int(page_no)-1)*20
  docs = db.session_info.find({'slug':session_pat}).sort("slug",-1).skip(skipno).limit(20)
  for doc in docs:
    record = {}
    session = doc['session']
    key = {'session':session}
    session_main_info = collection_sessions.find_one(key)
    session_ss_info = collection_ss.find_one(key)
    if session_ss_info and session_main_info:
      record['use_slug'] = session_main_info['use_slug']
      record['slug'] = doc['slug']
      record['user'] = session_main_info['username']
    record['session'] = session
    records.append(record)
  no_of_docs = db.session_info.find({'slug':session_pat}).count()
  no_of_pages = int(no_of_docs/20) + 1
  context = {'letter':letter,'no_of_pages':range(1,no_of_pages+1),'records':records,'page_no':page_no}
  return render(request, 'paginate.html',context)


Related Posts

1
2
3
4