1

我想在 tweepy 中对用户推文进行分页,以便每页仅显示 5 条推文或状态。我尝试添加以下代码,但它不起作用。现在的问题是,我怎样才能让它对每一页进行分页?

意见:

  def testdat(request):
      if request.method=="GET":
         treats=Cursor(api.list_timeline, owner=request.user, slug="").items(20)
         for treat in treats:
             treat.__getstate__()
             top=Twet(text= treat.text, author_screen_name= treat.author.screen_name, created_at= treat.created_at, source= treat.source) #Twet is the db created.
             top.save()
         treats=Cursor(api.list_timeline, owner=request.user, slug='').items(20)

         fab=Twet.objects.all()
         paginator=Paginator(fab, 5)
         try:
             page=request.GET.get('page', '1')
         except ValueError:
             page=1
         try:
           map=paginator.page(page)
         except (EmptyPage, InvalidPage):
             map=paginator.page(paginator.num_pages)
         return render_to_response('dashy.html',{'treats': treats, 'fab':fab, 'map':map},context_instance=RequestContext(request))

模板:

         {% block content %}

           {% for treat in treats %}  

            <center> <p> {{ treat.author.screen_name}} </p>
       <p>  {{ treat.text|safe }}  {{ treat.url }} </p>
       <p> {{ treat.created_at }}  Via: {{treat.source}} </p> </center>

           {% empty %} 

             <p> Sorry, No Update Could Be Loaded At This Time.  </p>
            {% endfor %} 

         <div class="pagination">
           <span class="step-links">
            {% if treats.has_previous %}
                  <a href="?page={{ treats.previous_page_number }}">previous</a>
            {% endif %}

              <span class="current">
              Page {{ treats.number }} of {{ treats.paginator.num_pages }}
             </span>

               {% if treats.has_next %}
                  <a href="?page={{ treats.next_page_number }}">next</a>
               {% endif %}
             </span>
           </div>
       {% endblock %}
4

1 回答 1

1

如果你不想在数据库中对保存为 Django 对象的推文进行分页,你可以使用django-pagination应用程序。

如果您想对 tweepy 返回的推文进行分页,tweepy 光标教程可能会有所帮助。

编辑:上面的 tweepy 光标教程链接不起作用。访问http://tweepy.readthedocs.org/en/v3.5.0/cursor_tutorial.html 获取教程。

于 2012-05-11T08:07:15.363 回答