本文介绍了django-pagination可以在每页上进行多个分页吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不能,那么是否还有其他选择(Django的本机分页或替代包)允许每页多个分页?

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?

我想显示大约5个对象的列表,每个对象都有自己的分页上下文.

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

为方便起见,这是django-pagination的文档.

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

推荐答案

我知道这篇文章很旧,但是仍然有用.以下内容适用于Django 1.9.

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

这是这样做的方法,

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)

上面重要的是'page1'和'page2'.

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

在模板中,

    {% 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 %}

再次使用"page1"和"page2"来区分每个模型的分页.

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

这篇关于django-pagination可以在每页上进行多个分页吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!