Question

I am using django pagination, as told in documentation:

view part is :

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})

and template is:

<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>

But it gives the error saying:

In template d:\programming\django_projects\kaasib\templates\jobs\list.html, error at line 32
Caught TypeError while rendering: 'Page' object is not iterable

I am new in django and this error seems general but very strange. Because in loop there is some other variable, not job. So please tell if anyone have any idea about it.

thanks

Était-ce utile?

La solution

The error should be clear - the variable you've called jobs actually contains a Page object from the paginator. Which is as it should be, as you assigned jobs to paginator.page(x). So obviously, it contains a Page.

The documentation shows what to do:

{% for job in jobs.object_list %}

etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top