如何在一页中对两个以上的对象列表进行分页-Django get_context_data()

时间:2018-07-02 06:11:50

标签: javascript jquery html django pagination

假设我在Views.py中使用objects_a有两个或两个以上的对象列表objects_bget_context_data() of Django,并且我想对object_a和objects_b进行分页,我粘贴了objects_list .html也如下所示,它只能对一个对象objects_a或objects_b进行分页,如何对两个对象进行分页?

我有两个以上的对象列表,四个或五个列表,如何在一页中分页两个以上的对象列表?非常感谢您的任何建议。

class ObjectListView(PaginationMixin, ListView):
    model = Object
    ordering = ('name', 'department')
    context_object_name = 'objects'
    template_name = ''

    paginate_by = 20

    def get_queryset(self, **kwargs):
        queryset = Permit.objects.all()
        return queryset

    def get_context_data(self, **kwargs):
        .................
        obejcts_a = ........
        obejcts_b = ........
        .................

        extra_context = {
            "objects_a": objects_a,
            "objects_b": objects_b,
        }

        kwargs.update(extra_context)
        return super().get_context_data(**kwargs)

这是我的objects_aobjects_b的objects_list.html, {%扩展了'base.html'%}

{% block content %}
  <nav aria-label="breadcrumb">
  </nav>
  <h3 class="mb-3">List of object A</h3>
  <div class="card">
    <table class="table mb-0">
      <thead>
        <tr>
          <th>Name</th>
          <th>Department</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {% for object in objects_a %}
          <tr>
            <td class="align-middle">{{ object.name }}</td>
            <td class="align-middle">{{ object.department.name }}</td>
          </tr>
        {% empty %}
        {% endfor %}
      </tbody>
    </table>
  </div>
<br>
  <h3 class="mb-3">List of object B</h3>
  <div class="card">
    <table class="table mb-0">
      <thead>
        <tr>
          <th>Name</th>
          <th>Department</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {% for object in objects_b %}
          <tr>
            <td class="align-middle">{{ object.name }}</td>
            <td class="align-middle">{{ object.department.name }}</td>
          </tr>
        {% empty %}
        {% endfor %}
      </tbody>
    </table>
  </div>

 <br>
{% if is_paginated %}
  <div class="pagination">
    {% if page_obj.has_previous %}
        <a href="?{{ page_obj.previous_page_number.querystring }}" class="prev">&lsaquo;&lsaquo;上一页</a>
    {% else %}
        <span class="disabled prev">&lsaquo;&lsaquo;上一页</span>
    {% endif %}
    {% for page in page_obj.pages %}
        {% if page %}
            {% ifequal page page_obj.number %}
                <span class="current page">{{ page }}</span>
                &nbsp;&nbsp;&nbsp;
            {% else %}
                <a href="?{{ page.querystring }}" class="page">{{ page }}</a>
                &nbsp;&nbsp;&nbsp;
            {% endifequal %}
        {% else %}
            ...
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <a href="?{{ page_obj.next_page_number.querystring }}" class="next">下一页 &rsaquo;&rsaquo;</a>
    {% else %}
        <span class="disabled next">下一页 &rsaquo;&rsaquo;</span>
    {% endif %}
  </div>
{% endif %}
{% endblock %}

0 个答案:

没有答案