D
D
DmSS19972021-07-16 17:31:25
Django
DmSS1997, 2021-07-16 17:31:25

How to make two paginations for two separate models on one page?

There is a task: on one page to display information from two models in the form of a table. Let's say 4 records should be displayed on one page. How to make separate pagination for these models in one controller? The models are called DailyOrders and Tenants.

Here is the code from views.py :

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


    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        aredators = Tenants.objects.all()
        paginator = Paginator(aredators, 2)
        page_number = self.request.GET.get('page')
        page_obj1 = paginator.get_page(page_number)

        context['page_obj1'] = page_obj1


I managed to correctly display this data on the page, but switching from one page to another does not work correctly, that is, if I switch from the first table to the second page in the first table, then the page will change in the second table, but it should not be.
60f196ff2fc4e326176519.png

Pagination in the template for the first table:
{% 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="{% spurl path=request.get_full_path query=request.GET set_query='page={{page_obj.previous_page_number}}'%}"
                           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" href="{% spurl path=request.get_full_path query=request.GET set_query='page={{ p }}'%}">{{ 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="{% spurl path=request.get_full_path query=request.GET set_query='page={{ p }}'%}">{{ p }}</a>
                    </li>
                    {% endif %}


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


For the second one (I thought if you change the names, it will work, but no, otherwise the code is absolutely identical here and there, except for the names page_obj and page_obj1):
{% if page_obj1.has_other_pages %}
            <nav aria-label="page_obj1 navigation example">
                <ul class="pagination justify-content-center my-3">
                    {% if page_obj1.has_previous %}
                    <li class="page-item    ">
                        <a class="page-link text-dark" href="{% spurl path=request.get_full_path query=request.GET set_query='page={{page_obj1.previous_page_number}}'%}"
                           tabindex="-1">Пердыдущая</a>
                    </li>
                    {% endif %}
                    {% for p in page_obj1.paginator.page_range %}

                    {% if page_obj1.number == p %}
                    <li class="page-item active" aria-current="page">

                        <a class="page-link text-dark bg-danger" href="{% spurl path=request.get_full_path query=request.GET set_query='page={{ p }}'%}">{{ p }}</a>
                    </li>
                    {% elif p > page_obj1.number|add:-3 and p < page_obj1.number|add:3 %}
                    <li class="page-item">
                        <a class="page-link text-dark " href="{% spurl path=request.get_full_path query=request.GET set_query='page={{ p }}'%}">{{ p }}</a>
                    </li>
                    {% endif %}


                    {% endfor %}
                    {% if page_obj1.has_next %}
                    <li class="page-item">
                        <a class="page-link text-dark " href="{% spurl path=request.get_full_path query=request.GET set_query='page={{ page_obj1.next_page_number }}'%}"
                           tabindex="-1">Следующая</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}

Please tell me how to solve the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-07-22
@DmSS1997

At the root of your problem lies the wrong design of the interface, in principle, there should not be two lists for different models on one page. The design of the ListView is based on the same simple postulate, which you break with a crutch with get_context_data. So it would be right to change the design. But if you want it wrong, you will have to abandon CBV, a ready-made paginator and other library goodies, and write all this yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question