Pagination and filters are not working correctly

Filters values ​​in views.py, then when you go to the next page, the filter is removed. Help me friends!

 if request.method == "POST":
        betForm = BetForm()
        title = request.POST.get("title")
        betAll = Bet.objects.filter(title__contains=title)
        paginator = Paginator(betAll, 10)
        page = request.GET.get('page')
        try:
            posts = paginator.page(page)
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)
        return render(request, 'bet/WebBet.html', {'page': page, 'posts': posts, 'home': paginator, 'form': betForm})
    else:
        betForm = BetForm()
        betAll = Bet.objects.all()
        paginator = Paginator(betAll, 10)
        page = request.GET.get('page')
        try:
            posts = paginator.page(page)
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)
        return render(request, 'bet/WebBet.html', {'page': page, 'posts': posts, 'home': paginator, 'form': betForm})

Pagination.html

<div class="pagination">
    <span class="step-links">
        {% if page.has_previous %}
            <a href="?page={{ page.previous_page_number }}">Previous</a>
        <span class="current">
            Page {{ page.number }} of {{ page.paginator.num_pages }}
        </span>
        {% endif %}
        {% if page.has_next %}
            <a href="?page={{ page.next_page_number }}">Next page</a>
        {% endif %}
    </span>
</div>

First, notice the basic difference between the GET and POST portion of your view.

In the POST section:

betAll = Bet.objects.filter(title__contains=title)

In the GET section:

Bet.objects.all()

You’re no longer filtering!

Briefly, what you need to do is ensure your title filter is available for every subsequent request.

You have a couple options, depending upon some external factors.

  1. Pass your title filter back to the view as a get variable on every request. This means that you will need to append that parameter to every URL being rendered, and retrieve it for use in the view.
  2. Store the current filter in the session on the POST, and retrieve it from the session in the GET.

Thank you so much! You can write the code, I can’t think of anything

The request.GET object allows you to access the query parameters from the URL.

You can then include that parameter (from either the GET or POST) in your context to be rendered in your template. Then add the proper reference to that parameter everywhere in your template that you are doing a request back to that view.
For example, in your template, you have:
<a href="?page={{ page.previous_page_number }}">Previous</a>
If you put the title in your context with the name title, then you would want to render that element something like this:
<a href="?page={{ page.previous_page_number }}&title={{title}}">Previous</a>
(However, you do need to make sure that the variable being passed to the template is urlencoded, and so you’d want to do a urldecode on it when you’re retrieving it in the view.)

1 Like

Thank you so much!
you helped me