【发布时间】:2018-10-20 11:57:45
【问题描述】:
我有这个功能,我可以从 Stackoverflow Api 获取数据,如下所示,我将它们显示在 html 表格中。 有没有办法对这个字典列表进行分页,以便每页显示 10 个结果?
views.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)
questions_list.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>
【问题讨论】:
-
就个人而言,我曾经添加datatables.net。分页会在前端通过datatable javascript库生成。
-
from django.core.paginator import Paginator,django的重点是everything都是内置的 -
@PanosAngelopoulos 好吧,你让我大吃一惊!不知道存在!谢谢!!!请创建一个答案,以便我关闭问题!
-
@DanaeVogiatzi 刚刚做到了。不客气。
标签: python django pagination