Question

I am trying to use the pagination lib shown here https://docs.djangoproject.com/en/dev/topics/pagination/. The issue I am having is that when I go the next page I am losing the state of my last GET request.

ex. www.mysite.com/search-results/?Make=BMW

Then when I press next page I lose the Make=BMW portion of my last get.

www.mysite.com/search-results/?page=2

Is there a way I can submit the new GET for my next page without listing that information in my url and causing it to lose the state ?

Here is how I am getting my query set and using the lib.

        listings = Listing.objects.all()

        paginator = Paginator(listings, 25)

        page = request.GET.get('page')
        try:
            listing_info = paginator.page(page)
        except PageNotAnInteger:
            listing_info = paginator.page(1)
        except EmptyPage:
            listing_info = paginator.page(paginator.num_pages)
Was it helpful?

Solution

I'm guessing you're using the <href> example in the docs you linked to in your template? i.e.:

<a href="?page={{ listing_info.next_page_number }}">next</a>

The problem with that is that it will only return your current url plus the query parameters you've provided, which is only the ?page parameter.

I think you can fix this with two different things. First, you need to get your Make parameter in your view and pass it to your template. Try adding something like:

make = request.GET.get('Make', None)
...
return render_to_response('list.html', {"listing_info": listing_info, "make": make})

Then, you'll need to adjust your href tag in your template so it also passes the make:

<a href="?Make={{ make }}?page={{ listing_info.next_page_number }}">next</a>

You may need to play with that a little and also deal with how to handle if it Make = None (if possible).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top