Python Django如何为目录列表分页
我有一个函数,从Stackoverflow Api中获取数据,如下所示,我将它们显示在html表中。 有没有一种方法可以对这个目录列表进行分页,以便每页显示10个结果 视图。py:Python Django如何为目录列表分页,python,django,pagination,Python,Django,Pagination,我有一个函数,从Stackoverflow Api中获取数据,如下所示,我将它们显示在html表中。 有没有一种方法可以对这个目录列表进行分页,以便每页显示10个结果 视图。py: def get_questions(request): context = {} url = 'https://api.stackexchange.com/2.2/questions' params = { 'fromdate':'1525858177', '
def get_questions(request):
context = {}
url = 'https://api.stackexchange.com/2.2/questions'
params = { 'fromdate':'1525858177',
'todate':'1525904006',
'order':'desc',
'sort':'activity',
'tagged':'python',
'site':'stackoverflow'
}
r = requests.get(url, params=params).json()
dataList = []
for item in r['items']:
dataList.append({
'owner': item['owner']['display_name'],
'title': item['title'],
'creation_date': datetime.datetime.fromtimestamp(float(item['creation_date'])),
'is_answered': item['is_answered'], # (yes / no)
'view_count': item['view_count'],
'score':item['score'],
'link':item['link'],
'answer_count':item['answer_count']
})
template = 'questions/questions_list.html'
context['data'] = dataList
return render(request,template,context)
<table id="myTable" class="table table-hover">
<thead>
<tr>
<th scope="col">Owner</th>
<th scope="col">Title</th>
<th scope="col">Creation date</th>
<th scope="col">Is answered</th>
<th scope="col">View count</th>
<th scope="col">Score</th>
</tr>
</thead>
<tbody>
{% for d in data %}
<tr>
<td>{{ d.owner }}</td>
<td><a href="{{ d.link }}" target="blank">{{ d.title }}</a></td>
<td>{{ d.creation_date }}</td>
<td>{{ d.is_answered|yesno:"yes,no" }}</td>
<td>{{ d.view_count }}</td>
<td>{{ d.score }}</td>
</tr>
{% endfor %}
</tbody>
</table>
问题列表。html:
def get_questions(request):
context = {}
url = 'https://api.stackexchange.com/2.2/questions'
params = { 'fromdate':'1525858177',
'todate':'1525904006',
'order':'desc',
'sort':'activity',
'tagged':'python',
'site':'stackoverflow'
}
r = requests.get(url, params=params).json()
dataList = []
for item in r['items']:
dataList.append({
'owner': item['owner']['display_name'],
'title': item['title'],
'creation_date': datetime.datetime.fromtimestamp(float(item['creation_date'])),
'is_answered': item['is_answered'], # (yes / no)
'view_count': item['view_count'],
'score':item['score'],
'link':item['link'],
'answer_count':item['answer_count']
})
template = 'questions/questions_list.html'
context['data'] = dataList
return render(request,template,context)
<table id="myTable" class="table table-hover">
<thead>
<tr>
<th scope="col">Owner</th>
<th scope="col">Title</th>
<th scope="col">Creation date</th>
<th scope="col">Is answered</th>
<th scope="col">View count</th>
<th scope="col">Score</th>
</tr>
</thead>
<tbody>
{% for d in data %}
<tr>
<td>{{ d.owner }}</td>
<td><a href="{{ d.link }}" target="blank">{{ d.title }}</a></td>
<td>{{ d.creation_date }}</td>
<td>{{ d.is_answered|yesno:"yes,no" }}</td>
<td>{{ d.view_count }}</td>
<td>{{ d.score }}</td>
</tr>
{% endfor %}
</tbody>
</table>
所有者
标题
创建日期
答案是
查看次数
分数
{数据%中d的%s}
{{d.owner}
{{d.creation_date}
{d.is_回答{124; yesno:“是的,不是”}
{{d.view_count}
{{d.score}
{%endfor%}
Django提供了几个类,可帮助您管理分页数据,即通过“上一页/下一页”链接拆分的数据。这些类位于django/core/paginator.py中
为Paginator提供一个对象列表,再加上您希望在每页上显示的项目数,它为您提供了访问每页项目的方法:
>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo']
>>> p = Paginator(objects, 2)
>>> p.count
4
>>> p.num_pages
2
>>> type(p.page_range)
<class 'range_iterator'>
>>> p.page_range
range(1, 3)
>>> page1 = p.page(1)
>>> page1
<Page 1 of 2>
>>> page1.object_list
['john', 'paul']
>>> page2 = p.page(2)
>>> page2.object_list
['george', 'ringo']
>>> page2.has_next()
False
>>> page2.has_previous()
True
>>> page2.has_other_pages()
True
>>> page2.next_page_number()
Traceback (most recent call last):
...
EmptyPage: That page contains no results
>>> page2.previous_page_number()
1
>>> page2.start_index() # The 1-based index of the first item on this page
3
>>> page2.end_index() # The 1-based index of the last item on this page
4
>>> p.page(0)
Traceback (most recent call last):
...
EmptyPage: That page number is less than 1
>>> p.page(3)
Traceback (most recent call last):
...
EmptyPage: That page contains no results
>>来自django.core.paginator导入paginator
>>>objects=['john','paul','george','ringo']
>>>p=分页器(对象,2)
>>>p.计数
4.
>>>p.num_页
2.
>>>类型(p.page\u范围)
>>>p.page_范围
范围(1,3)
>>>第1页=第(1)页
>>>第1页
>>>第1页对象列表
[约翰,保罗]
>>>第2页=第(2)页
>>>第2页对象列表
['george','ringo']
>>>第2页。你有下一页吗
假的
>>>第2页上一页
真的
>>>第2页。是否有其他页()
真的
>>>第2页下一页页码()
回溯(最近一次呼叫最后一次):
...
EmptyPage:该页面不包含任何结果
>>>第2页上一页页码()
1.
>>>page2.start_index()#此页面上第一项的基于1的索引
3.
>>>page2.end_index()#此页面上最后一项的基于1的索引
4.
>>>第页(0)
回溯(最近一次呼叫最后一次):
...
EmptyPage:该页码小于1
>>>第(3)页
回溯(最近一次呼叫最后一次):
...
EmptyPage:该页面不包含任何结果
我希望这些示例代码片段有助于解决您的查询 我的建议是使用
它很容易安装,但分页仍将在前端完成
简单地加上
<link rel="stylesheet" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" />
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"> </script>
您基本上可以使用DRF,覆盖基于类的视图模型视图集,添加分页类。然后您可以轻松自定义分页显示100、20、10等等
from rest_framework import views, viewsets, authentication, permissions,pagination
class A (viewsets.ModelViewSet):
pagination_class = pagination.PageNumberPagination
page_size = 100
def get_questions(self, request, *args, **kwargs):
#Your code here
#intercete the code and inject this here
if request.GET.get('page', None) is not None:
self.page_size = request.GET.get('page_size',self.page_size)
page = self.paginate_queryset(self.queryset.order_by(order_by))
else:
#Something else
就我个人而言,我曾经补充说。分页将通过datatable javascript库在前端生成。
从django.core.paginator导入paginator
,django的重点是构建所有内容-in@PanosAngelopoulos你让我大吃一惊!我不知道它的存在!非常感谢。请创建一个答案,以便我可以关闭该问题@DanaeVogiatzi就是这么做的。不客气。这是错误的。为什么?您正在将buld数组发送到前端,并让前端自己对其进行分页。所以最好在后端分页,并延迟加载到前端