[Answered ]-Sorting and paginating object at the same time in django; after paginating the sort gets reset

1👍

The reason this happens is because you "drop" the URL parameters regarding sorting and searching. You should each time add these to the pagination URLs.

In the view you can make a querystring for all the parameters except page with:

def home(request):
    qd = request.GET.copy()
    qd.pop('page', None)
    querystring = qd.urlencode()
    # …
    context = {'querystring': querystring, 'wallpapers': wallpapers, 'page_obj': wallpapers, 'is_paginated': True, 'paginator': wallpaper_paginator, 'WALL': WALL}
    return render(request, "Wallpaper/Home.html", context)

then in the template, you render the links to the other pages with:

<a href="/?page={{ page_obj.previous_page_number }}&amp;{{ querystring }}" class="page-link" tabindex="-1" aria-disabled="true">Previous</a>

and this for all URL links.

Leave a comment