我想在 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 %}