Skip to main content
 首页 » 编程设计

python之Django 如何对字典列表进行分页

2025年05月04日36wuhuacong

我有这个功能,我可以从 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>  

请您参考如下方法:

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 

我希望这些示例代码片段有助于解决您的查询!