Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 472767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:07:13+00:00 2026-05-13T00:07:13+00:00

I’m using this code for my pagination, and I’d like the user’s choice to

  • 0

I’m using this code for my pagination, and I’d like the user’s choice to be persistent throughout the site (this has been solved so far)…the only problem now is that the session variable now is permanent until the session is cleared by closing the browser. Also, how can I get the adjacent pages displayed…like in the digg-style Django paginator. I haven’t been able to make sense of how to implement this into my code.

The code is as follows:

from django.core.paginator import Paginator, InvalidPage, EmptyPage

def paginate(request, object_list, paginate_by=10):
   try:
      if "per_page" in request.session:
        per_page = request.session["per_page"]
      else:
        request.session["per_page"] = int(request.REQUEST['p'])
        per_page = request.session["per_page"]
        request.session.set_expiry(0)
   except:
      per_page = 10

   paginator = Paginator(object_list, per_page)

   try:
      page = int(request.GET.get('page', '1'))
   except ValueError:
      page = 1

   try:
      items = paginator.page(page)
   except (EmptyPage, InvalidPage):
      items = paginator.page(paginator.num_pages)

   return items

Then in my template I have this to render the pagination links:

<div class="pagination" align="center"> 
 <span class="step-links"> 
  {% if items.has_previous %} 
    <a href="?page={{ items.previous_page_number }}">previous</a> 
  {% endif %} 
  <span class="current"> 
    Page {{ items.number }} of {{ items.paginator.num_pages }} 
  </span> 
  {% if items.has_next %} 
    <a href="?page={{ items.next_page_number }}">next</a> 
  {% endif %} 
 </span> 
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:07:13+00:00Added an answer on May 13, 2026 at 12:07 am

    You can achieve this by enabling sessions.

    I recommend reading through the chapter Sessions, Users, and Registration on the djangobook website.


    Edit: Now that you’ve enabled sessions, I think the problem is the hyperlinks in the template. Use an ampersand to separate multiple parameters in a url, for example

     <a href="?p={{ request.session.per_page }}&page={{ items.next_page_number }}">next</a> 
    

    Edit 2: I’m not sure if I understand what the problem is with the session expiry. The line that sets the session to expire when the browser closes is request.session.set_expiry(0). See the django docs on Using Sessions in views if you want to change that.

    To make a Digg style paginator, you need to write a function that takes the current page number and the total number of pages, and returns a list of page numbers. Then, in the template, loop through the page numbers and construct links to the pages.

    A list of lists of page numbers would allow you to split the page numbers into groups, eg

    [[1,2], [20,21,22,23,24], [30,31]]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 3-tier architecture isn't language dependent. It's a way of structuring… May 14, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer I've solved the problem. SELECT @final = CONVERT(VARCHAR(80), cast(@lon as… May 14, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer Reason: These are added by the site module. I believe… May 14, 2026 at 6:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.