假设我在Views.py中使用objects_a
有两个或两个以上的对象列表objects_b
和get_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_a
和objects_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">‹‹上一页</a>
{% else %}
<span class="disabled prev">‹‹上一页</span>
{% endif %}
{% for page in page_obj.pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?{{ page.querystring }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?{{ page_obj.next_page_number.querystring }}" class="next">下一页 ››</a>
{% else %}
<span class="disabled next">下一页 ››</span>
{% endif %}
</div>
{% endif %}
{% endblock %}