0

I use django-paginator. First page comes sorted. But when go two or other pages sorting is broken. I use this view code:

views.py

def mostviewdaily(request):
    poll = Poll.objects.filter(created_date__gte=datetime.now() - timedelta(hours=24)).order_by('-totalvotes')
    paginator = Paginator(poll, 30)
    page = request.GET.get('page')
    poll = paginator.get_page(page)
    yorumlar = Comment.objects.all()
    return render(request, "poll/popular.html", {"poll": poll,"yorumlar": yorumlar, })

here's popular.html file. normally these file working good first page. but when i go second or third page, list are coming not sorted.

<div class="scrollbar" id="style-3">
  <ul id="topic-list" class="topic-list">
    {% for anketler in poll %}
    <li><a class="noajax" href="/poll/vote/{{ anketler.id }}">{{ anketler.title }} <small
      class="pull-right mr-2"
      title="{{ anketler.totalvotes }} Votes | {{ anketler.comments.count }} Comments "><i
      class="fa fa-check-circle"></i> {{ anketler.totalvotes }} | <i
      class="fa fa-comments"></i>{{ anketler.comments.count }}
    </small> </a>
  </li>
  <hr>
  {% endfor %}
</ul>
</div>

<nav aria-label="Page navigation example">
  <ul class="pagination justify-content-center">
    {% if poll.has_previous %}
    <li class="page-item">
      <a class="page-link" href="&page=1">first</a>
    </li>
    <li class="page-item">
      <a class="page-link" href="&page={{ poll.previous_page_number }}">previous</a>
    </li>
    {% endif %}
    {% for l in  poll.paginator.page_range %}
    {% if l <= poll.number|add:1 and l >= poll.number|add:-1 %}
    <li class="page-item"><a class="page-link" href="?page={{ forloop.counter }}">{{ forloop.counter }}</a></li>
    {% endif %}
    {% endfor %}
    {% if poll.has_next %}
    <li class="page-item">
      <a class="page-link" href="?results={{ query }}&page={{ poll.next_page_number }}">next</a>
    </li>
    <li class="page-item">
      <a class="page-link" href="?results={{ query }}&page={{ poll.paginator.num_pages }}">last</a>
    </li>
    {% endif %}
  </ul>
</nav>

How can i solve this? Thanks

Riyas Ac
  • 1,553
  • 1
  • 9
  • 23
Aytek
  • 224
  • 4
  • 16
  • what does the rest of `poll/popular.html` look like? – Ben Apr 14 '20 at 18:06
  • i edited and add popular.html file. thanks – Aytek Apr 14 '20 at 18:57
  • When you click on the "next" url, what does it look like? It looks like your `{% if poll.has_next %}` section includes urls with `?results={{ query }}` but I'm not seeing where you're including that variable in the context? – Ben Apr 14 '20 at 21:02
  • @Ben im new on django. sorry I can not understand exactly. my list in "poll" variable in view.py. i sorted this "totalvotes". its work at first page. i found pagenavi html codes in google. How should I use these codes to come in sorted on the next page? – Aytek Apr 14 '20 at 21:42
  • @Ben how can i get list query in view.py? As I have given in the example above, I have daily and weekly lists. I think I need to define a query for each. but i don't know how to do it. Can you give me an example from my view.py? Thanks a lot! – Aytek Apr 15 '20 at 02:02
  • This [link](https://stackoverflow.com/questions/63506609/how-organize-pagination-with-a-large-number-of-pages-in-django-project/63507365#63507365) may help you. This link is best practices for large datasets. – Riyas Ac Sep 01 '20 at 16:47

0 Answers0