python - Django如何对字典列表进行分页
问题描述
我有这个函数,可以从 Stackoverflow Api 获取数据,如下所示,并将它们显示在 html 表中。有没有办法对这个字典列表进行分页,以便在每页上显示 10 个结果?
视图.py:
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)
问题列表.html:
<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>
解决方案
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
我希望这些示例代码片段有助于解决您的查询!
推荐阅读
- python - python从文件中读取二维列表中的多行到一维
- javascript - 当方法不返回任何内容时,然后不是函数 - Javascript
- python - 如何在用户输入中使用 3 个预先确定的单词来打印预先确定的响应
- python - 如何在将函数应用于两列时保留我的熊猫数据框的其他列
- laravel - 在 Laravel >=5.6 中获取 `LoggerInterface` 实例(替换为 `Log::getMonolog()`)?
- azure-functions - Azure Functions 存储帐户不同于队列
- wordpress - Wordpress 使用 PHP 有条件地隐藏或禁用评论框
- loops - 遍历元素从 1 到 n 小于 O(n)
- docusignapi - Docusign Gen for Salesforce 的模板 ID
- vb.net - 如何复制 Windows 资源管理器中的选定文件?