使用 class 基本视图不显示 Django 分页
Django pagination not showing using class base view
您好,我正在寻找解决方案,但没有解决我的问题,我不知道为什么,但 html 文件中没有显示页码。这是结构代码:
class ListEmployeeActivity(ListView, LoginRequiredMixin):
paginate_by = 1
model = EmployeePersonalDetailsModel
template_name = 'layout/employee_list_layout.html'
context_object_name = 'datalist'
def get_queryset(self):
return self.model.objects.prefetch_related('ecdm_fk_rn').all()
html 文件:
{% if is_paginated %}
<div class="pagination pagination-centered">
<ul>
{% if datalist.has_previous %}
<li><a href="?page={{ datalist.previous_page_number }}"><i class="icon-double-angle-left"></i></a></li>
{% endif %}
{% for total_pages in datalist.paginator.page_range %}
<li><a href="?page={{ total_pages }}">1</a></li>
{% endfor %}
{% if datalist.has_next %}
<li><a href="?page={{ datalist.next_page_number }}"><i class="icon-double-angle-right"></i></a></li>
<li><a href="?page={{ datalist.paginator.num_pages }}">Last</a></li>
{% endif %}
</ul>
</div>
{% else %}
<h3>Pagination not working.</h3>
{% endif %}
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
class UserListView(ListView):
model = User
template_name = 'core/user_list.html'
context_object_name = 'users'
paginate_by = 10
queryset = User.objects.all()
HTML 文件。
<table class="table table-bordered">
<thead>
<tr>
<th>Username</th>
<th>First name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.username }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
您好,我正在寻找解决方案,但没有解决我的问题,我不知道为什么,但 html 文件中没有显示页码。这是结构代码:
class ListEmployeeActivity(ListView, LoginRequiredMixin):
paginate_by = 1
model = EmployeePersonalDetailsModel
template_name = 'layout/employee_list_layout.html'
context_object_name = 'datalist'
def get_queryset(self):
return self.model.objects.prefetch_related('ecdm_fk_rn').all()
html 文件:
{% if is_paginated %}
<div class="pagination pagination-centered">
<ul>
{% if datalist.has_previous %}
<li><a href="?page={{ datalist.previous_page_number }}"><i class="icon-double-angle-left"></i></a></li>
{% endif %}
{% for total_pages in datalist.paginator.page_range %}
<li><a href="?page={{ total_pages }}">1</a></li>
{% endfor %}
{% if datalist.has_next %}
<li><a href="?page={{ datalist.next_page_number }}"><i class="icon-double-angle-right"></i></a></li>
<li><a href="?page={{ datalist.paginator.num_pages }}">Last</a></li>
{% endif %}
</ul>
</div>
{% else %}
<h3>Pagination not working.</h3>
{% endif %}
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
class UserListView(ListView):
model = User
template_name = 'core/user_list.html'
context_object_name = 'users'
paginate_by = 10
queryset = User.objects.all()
HTML 文件。
<table class="table table-bordered">
<thead>
<tr>
<th>Username</th>
<th>First name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.username }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}