D
D
DmSS19972021-07-02 12:51:50
Django
DmSS1997, 2021-07-02 12:51:50

How to make pagination work properly when using django-filters?

It was necessary to implement a filter on the site for records. Before that, pagination for posts was also implemented. I figured out the filter and did it, the records are perfectly filtered, but when switching to another page, the filter was not saved. I decided to unload all data from the GET request in the link to the next page, and now the filtering was saved when moving to the second page.
Here is the code from Views

class MainPage(ListView):
    model = DailyOrders
    template_name = 'Catalog/main_page.html'
    context_object_name = 'orders'
    paginate_by = 3
    allow_empty = True

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = ClientsForm()
        context['filter'] = DailyOrdersFilter(self.request.GET, queryset = DailyOrders.objects.all())
        return context

   def get_queryset(self):
        qs = DailyOrders.objects.all()
        word = DailyOrdersFilter(self.request.GET, queryset=qs)
        return word.qs


Here is how in html
{% for order in orders%}
                    <tr>
                        <td>{{order.data|date:"d.m.Y"}}</td>
                        <td>{{order.city}}</td>
                        <td>{{order.name_of_company}}</td>
                        <td>{{order.kind_of_activity}}</td>
                        <td>{{order.details}}</td>
                        <td>{{order.square}}</td>
                        <td>{{order.floor}}</td>
                        <td>{{order.requirements}}</td>
                        <td>{{order.district}}</td>
                        <td>{{order.budget}}</td>
                    </tr>
                    {% endfor %}
                </table>
            </div>
            {% if page_obj.has_other_pages %}
            <nav aria-label="Page navigation example">
                <ul class="pagination justify-content-center my-3">
                    {% if page_obj.has_previous %}
                    <li class="page-item    ">
                        <a class="page-link text-dark" href="?page={{page_obj.previous_page_number}}{% if request.GET%}&{{ request.GET.urlencode }}{% endif %}"
                           tabindex="-1">Пердыдущая</a>
                    </li>
                    {% endif %}
                    {% for p in page_obj.paginator.page_range %}

                    {% if page_obj.number == p %}
                    <li class="page-item active" aria-current="page">
                        <a class="page-link text-dark bg-danger border-dark" href="?page={{p}}{% if request.GET%}&{{ request.GET.urlencode }}{% endif %}">{{p}}</a>
                    </li>
                    {% elif p > page_obj.number|add:-3 and p < page_obj.number|add:3 %}
                    <li class="page-item"><a class="page-link text-dark " href="?page={{p}}{% if request.GET %}&{{ request.GET.urlencode}}{% endif %}">{{p}}</a></li>
                    {% endif %}


                    {% endfor %}
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link text-dark " href="?page={{page_obj.next_page_number}}{% if request.GET%}&{{ request.GET.urlencode }}{% endif %}"
                           tabindex="-1">Следующая</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}


But the problem is that now you can’t go anywhere further from the second page, because absolutely everything is saved in the url and it’s impossible to make a transition. (Here I tried to go from the second page to the third)
http://127.0.0.1:8000/?page=3&page=2&city=Казань&square_for_filter_min=&square_for_filter_max=&floor=&district=&budget_for_filter_min=&budget_for_filter_max=

Please help me figure out how to do this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-07-02
@DmSS1997

DmSS1997 this is an elegant solution there is django-spurl links to other pages are done like this

{% load spurl %}

<a class="page-link" href="{% spurl query=request.GET set_query='page={{ p }}'%}">{{ p }}</a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question