Search code examples
pythondjangodjango-pagination

How to make Django pagination search engine friendly?


I am wondering how can I make Django pagination search engine friendly, like: object/224 instead of object?page=224

Also, anyone has an idea why it's not by default search engine friendly!?


Solution

  • Adjust your URL:

    (r'object/(?P<page>\d+)/$','listing')
    

    Then adjust your view (here I am using the sample from the documentation):

    def listing(request,page):
        contact_list = Contacts.objects.all()
        paginator = Paginator(contact_list, 25) # Show 25 contacts per page
    
        # page = request.GET.get('page') not needed
        try:
            contacts = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            contacts = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            contacts = paginator.page(paginator.num_pages)
    
        return render_to_response('list.html', {"contacts": contacts})