开发者

Object not iterable with django's pagination

开发者 https://www.devze.com 2023-01-09 16:54 出处:网络
I have a template showing a list of events. To prepare list of events I\'m using generic views, and set \'paginate_by\' parameter. Strangely when I load my page I see :

I have a template showing a list of events. To prepare list of events I'm using generic views, and set 'paginate_by' parameter. Strangely when I load my page I see :

TemplateSyntaxError at /event/latest/
Caught an exception while rendering: 'int' object is not iterable

in 9th line of pagination.html template :

{% if is_paginated %}
{% load i18n %}
<div class="pagination">
    {% if page_obj.has_previous %}
        <a href="?page={{ page_obj.previous_page_number }}{{ getvars }}" class="prev">&lt;&lt; Prev</a>
    {% else %}
        <span class="disabled prev">&lt;&lt; Prev开发者_JAVA技巧</span>
    {% endif %}
#here {% for page in pages %}
        {% if page %}
            {% ifequal page page_obj.number %}
                <span class="current page">{{ page }}</span>
            {% else %}
                <a href="?page={{ page }}{{ getvars }}" class="page">{{ page }}</a>
            {% endifequal %}
        {% else %}
            ...
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <a href="?page={{ page_obj.next_page_number }}{{ getvars }}" class="next">Next &gt;&gt;</a>
    {% else %}
        <span class="disabled next">Next &gt;&gt;</span>
    {% endif %}
</div>
{% endif %}

Here is my view :

def events_nearest(request):
    events = Event.nearest.all()

    return object_list(request,
            queryset = events,
            extra_context = {'title': 'Nearest events'},
            paginate_by = 12,
            template_name = 'event/events_date.html')

And model :

class NearestManager(models.Manager):
    def get_query_set(self):
        return super(NearestManager, self).get_query_set().order_by('-data')

class Event(models.Model):        
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=120)
    slug = models.SlugField(max_length=255, unique=True, verbose_name='Slug')
    about = models.TextField()
    city = models.ForeignKey(City)

    objects = models.Manager()
    nearest = NearestManager()

Any ideas what can cause this ?


pages variable is the number of pages, which is int and hence the error: 'int' object is not iterable

you should be looping over page_range

{% for page in page_range %}


I met the same error. There is a note at https://docs.djangoproject.com/en/dev/topics/pagination/

Changed in Django Development version: Previously, you would need to use {% for contact in contacts.object_list %}, since the Page object was not iterable.

So {% for page in pages.object_list %} could probably solve your problem.


For anybody stumbled upon this post:

As with Django 1.4 and later (as far as I know), the iterable object for pagination is now paginator.page_range , i.e. the for loop should be

{% for page_num in paginator.page_range %}


In your error line #9 {% for page in pages %} what exactly is pages

Can't see it in your code anywhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消