How to paginate more than two object lists in one page - Django get_context_data()


How to paginate more than two object lists in one page - Django get_context_data()



Suppose I have two or more than two object lists objects_a and objects_b in Views.py with get_context_data() of Django, and I would like to paginated for objects_a and objects_b, I pasted my objects_list.html as below too, which can only paginated one of objects, objects_a or objects_b, how to paginated both of objects?


objects_a


objects_b


get_context_data() of Django



I have more than two object lists, four or five lists, how to paginate more than two object lists in one page? Thank you so much for any advice.


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)



Here is my objects_list.html of objects_a and objects_b,
{% extends 'base.html' %}


objects_a


objects_b


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









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?