我正在使用django分页,如文档中所述:
查看部分是:
def list(request):
job_list = Job.objects.all()
paginator = Paginator(job_list, 25) # Show 25 jobs per page
page = request.GET.get('page',1)
try:
jobs = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
jobs = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
jobs = paginator.page(paginator.num_pages)
return render_to_response('jobs/list.html', {"jobs": jobs})
和模板是:
<div>
{% for job in jobs %}
{# Each "contact" is a Contact model object. #}
{{ job.title|upper }}<br />
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
<a href="?page={{ contacts.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if contacts.has_next %}
<a href="?page={{ contacts.next_page_number }}">next</a>
{% endif %}
</span>
</div>
</div>
但它给出了错误说法:
In template d:\programming\django_projects\kaasib\templates\jobs\list.html, error at line 32
Caught TypeError while rendering: 'Page' object is not iterable
我是django的新手,这个错误似乎很普遍,但非常奇怪。因为在循环中有一些其他变量,而不是作业。所以请告诉我们是否有任何想法。
感谢
答案 0 :(得分:3)
错误应该是明确的 - 您称之为jobs
的变量实际上包含来自分页器的Page
对象。当您将jobs
分配给paginator.page(x)
时,应该是这样的。很明显,它包含一个Page。
documentation显示了该怎么做:
{% for job in jobs.object_list %}
等