TemplateView 支持分页吗?
Does TemplateView supports pagination?
如何使用 Templateview 实现分页?下面的视图是基于 class 的视图,我在其中使用 Templateview 列出网络(我从另一个应用程序 api 调用它)。这里我没有使用模型,所以我不能使用 Listview,因为我没有查询集。
class classView(TemplateView):
template_name = 'view_network.html'
# paginate_by = 5
context_object_name = 'networks'
paginator = Paginator('networks', 5)
def get_context_data(self, **kwargs):
...
...
return context
在 html 页面我添加了这个:
<span class="step-links" style="margin-left: 30%;
margin-bottom: 0px;
margin-top: 0px;
width: 100%;">
<div style="text-align: justify"></div>
<table style="width: 50%">
<tr>
{% if page_obj.has_previous %}
<td><a href="?page=1">« First</a></td>
<td> <a href="?page={{ page_obj.previous_page_number }}">Previous</a></td>
{% endif %}
<td> Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</td>
{% if page_obj.has_next %}
<td><a href="?page={{ page_obj.next_page_number }}">Next</a></td>
<td><a href="?page={{ page_obj.paginator.num_pages }}">Last »</a></td>
{% endif %}
</tr>
</table>
</div>
</span>
</div>
TemplateView 不支持分页,您必须使用 ListView 才能使用“paginate_by = 5” Here 有助于 link 检查所有 attributes/methods每个 class 基于通用视图。
您可以在 get_context_data()
函数中实现分页。假设 networks
是一个对象列表。
def get_context_data(self, **kwargs):
page_size = 5
paginator = Paginator(networks, page_size)
page = request.GET.get('page', 1)
page_obj = paginator.page(page)
context = {
"page_obj": page_obj
}
如何使用 Templateview 实现分页?下面的视图是基于 class 的视图,我在其中使用 Templateview 列出网络(我从另一个应用程序 api 调用它)。这里我没有使用模型,所以我不能使用 Listview,因为我没有查询集。
class classView(TemplateView):
template_name = 'view_network.html'
# paginate_by = 5
context_object_name = 'networks'
paginator = Paginator('networks', 5)
def get_context_data(self, **kwargs):
...
...
return context
在 html 页面我添加了这个:
<span class="step-links" style="margin-left: 30%;
margin-bottom: 0px;
margin-top: 0px;
width: 100%;">
<div style="text-align: justify"></div>
<table style="width: 50%">
<tr>
{% if page_obj.has_previous %}
<td><a href="?page=1">« First</a></td>
<td> <a href="?page={{ page_obj.previous_page_number }}">Previous</a></td>
{% endif %}
<td> Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</td>
{% if page_obj.has_next %}
<td><a href="?page={{ page_obj.next_page_number }}">Next</a></td>
<td><a href="?page={{ page_obj.paginator.num_pages }}">Last »</a></td>
{% endif %}
</tr>
</table>
</div>
</span>
</div>
TemplateView 不支持分页,您必须使用 ListView 才能使用“paginate_by = 5” Here 有助于 link 检查所有 attributes/methods每个 class 基于通用视图。
您可以在 get_context_data()
函数中实现分页。假设 networks
是一个对象列表。
def get_context_data(self, **kwargs):
page_size = 5
paginator = Paginator(networks, page_size)
page = request.GET.get('page', 1)
page_obj = paginator.page(page)
context = {
"page_obj": page_obj
}