Can django-pagination do multiple paginations per page?

Deadly 提交于 2019-12-14 04:27:19

问题


If it can't then are there any other alternatives (either Django's native pagination or an alternate package) that allows multiple paginations per page?

I would like to display a list of about 5 objects with each object having its own pagination context.

For convenience, here is the documentation for django-pagination.


回答1:


I know this post is old, but it is still relevant. The following will work for Django 1.9.

This is how to do it,

views.py

def myview():
    Model_one = Model.objects.all()
    paginator = Paginator(Model_one, 6)
    page = request.GET.get('page1')
    try:
        Model_one = paginator.page(page)
    except PageNotAnInteger:
        Model_one = paginator.page(1)
    except EmptyPage:
        Model_one = paginator.page(paginator.num_pages)

    Model_two = Model_other.objects.all()
    paginator = Paginator(Model_two, 6)
    page = request.GET.get('page2')
    try:
        Model_two = paginator.page(page)
    except PageNotAnInteger:
        Model_two = paginator.page(1)
    except EmptyPage:
        Model_two = paginator.page(paginator.num_pages)

    context = {'Model_one': Model_one, 'Model_two': Model_two}
    return render(request, 'template.html', context)

The important thing above is the 'page1' and 'page2'.

In the template,

    {% if model_one %}
      <div class="col-md-12 well">
        {% for item in model_one %}
        ..... iterates through model_one.....
        {% endfor %}
        <span class="step-links pagination">
            {% if model_one.has_previous %}
                <a href="?page1={{ model_one.previous_page_number }}"> previous </a>
            {% endif %}
            <span class="current">
                Page {{ model_one.number }} of {{ model_one.paginator.num_pages }}
            </span>
            {% if model_one.has_next %}
                <a href="?page1={{ model_one.next_page_number }}"> next </a>
            {% endif %}
        </span>
      </div>
      {% endif %}
      {% if model_two %}
      <div class="col-md-12 well">
        {% for item in model_two %}
        ..... iterates through model_two.....
        {% endfor %}
        <span class="step-links pagination">
            {% if model_two.has_previous %}
                <a href="?page2={{ model_two.previous_page_number }}"> previous </a>
            {% endif %}
            <span class="current">
                Page {{ model_two.number }} of {{ model_two.paginator.num_pages }}
            </span>
            {% if model_two.has_next %}
                <a href="?page2={{ model_two.next_page_number }}"> next </a>
            {% endif %}
        </span>
      </div>
      {% endif %}

Again using 'page1' and 'page2' to distinguish the pagination for each model.




回答2:


Sure, it is possible. I can't speak for the django-pagination package, but it definitely is possible using the default Django's Paginator class. You can create as many of them as you want inside your view. Just choose different GET parameters to specify the page for each of them and you're good to go.



来源:https://stackoverflow.com/questions/10511873/can-django-pagination-do-multiple-paginations-per-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!