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

  • Home
  • SEARCH
  • 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 3222348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:58:50+00:00 2026-05-17T15:58:50+00:00

I’m having some trouble with the django generic object_list function’s pagination not really being

  • 0

I’m having some trouble with the django generic object_list function’s pagination not really being “smart” enough to compensate my daftness.

I’m trying to do a url for listing with optional arguments for page number and category.
The url in urls.py looks like this:

url(r'^all/(?:(?P<category>[-\w]+)/page-(?P<urlpage>\d+))?/$',
views.listing,
),

The category and urlpage arguments are optional beacuse of the extra “(?: )?” around them and that works nicely.
views.listing is a wrapper function looking like this( i don’t think this is where my problem occurs):

def listing(request,category="a-z",urlpage="1"):
extra_context_dict={}
if category=="a-z":
    catqueryset=models.UserProfile.objects.all().order_by('user__username')
elif category=="z-a":
    catqueryset=models.UserProfile.objects.all().order_by(-'user__username')
else:
    extra_context_dict['error_message']='Unfortunately a sorting error occurred, content is listed in alphabetical order'
    catqueryset=models.UserProfile.objects.all().order_by('user__username')
return object_list(
                    request,
                    queryset=catqueryset,
                    template_name='userlist.html',
                    page=urlpage,
                    paginate_by=10,
                    extra_context=extra_context_dict,
                    )

In my template userlist.html I have links looking like this (This is where I think the real problem lies):

{%if has_next%}
  <a href=page-{{next}}>Next Page> ({{next}})</a>
{%else%}

Instead of replacing the page argument in my url the link adds another page argument to the url. The urls ends up looking like this “/all/a-z/page-1/page-2/

It’s not really surprising that this is what happens, but not having page as an optional argument actually works and Django replaces the old page-part of the url.

I would prefer this DRYer (atleast I think so) solution, but can’t seem to get it working.
Any tips how this could be solved with better urls.py or template tags would be very appreciated.

(also please excuse non-native english and on the fly translated code. Any feedback as to if this is a good or unwarranted Stack-overflow question is also gladly taken)

  • 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-17T15:58:51+00:00Added an answer on May 17, 2026 at 3:58 pm

    You’re using relative URLs here – so it’s not really anything to do with Django. You could replace your link with:

    <a href="/all/a-z/page-{{ next }}">Next Page> ({{ next }})</a>
    

    and all would be well, except for the fact that you’d have a brittle link in your template, which would break as soon as you changed your urls.py, and it wouldn’t work unless category happened to be a-z.

    Instead, use Django’s built-in url tag.

    <a href="{% url views.listing category next %}">Next Page> ({{ next }})</a>
    

    To make that work, you’ll have to pass your category into the extra_context_dict, which you create on the first line of your view code:

    extra_context_dict = { 'category': category }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.